Monday, December 5, 2011

Regular Expression

A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager.

? - Matches zero or one of preceding element
eg.: colou?r  =  color 
                          colour
                          coloour (wrong)

* - Matches zero or more of the preceding elements
eg.: ab*c  =  ac
                     abc
                    abbbc
                    abcdefc

+ - Matches one or more of preceding element
eg.: ab+c  =  abc
                      ac (wrong)
                      abbbc

. - Matches any single character
eg.: .at  =  hat
                  cat
                  bat

[ ] - Matches single character contained within the brackets.
eg.: [hc]  =  hat
                    cat
                    bat (wrong)

[^] - Matches a single character that is not contained within the bracket.
eg.: [^b]at  =  cat
                       hat
                       bat (wrong)

^ - Starting position within the string.

$ - ending position within string.

No comments:

Post a Comment