serving the solutions day and night

Pages

Saturday, April 3, 2010

Regular Expressions

Pattern Description
[]Matches any single character
[ae] "a" in "orange", "a" and "e" in "lane"
Character Classes
[^]Not matches any single character
[^aeio] "r", "g","n" in "region"
Character Classes
[start - end]Matches any single character in the range from start to end.
[A-Z] "A", "B" in "AB123"
Character Classes
.Matches any single character except \n.
a.e => "ave" in "nave" , "ate" in "water"
Character Classes
*Matches the previous element zero or more times.
"be+" => "bee" in "been", "be" in "bent"
Quantifiers
+Matches the previous element one or more times.
\\d*\\.\\d = > ".0", "19.9", "219.9"
Quantifiers
?Matches the previous element zero or one time.
"rai?n" = > "ran", "rain"
Quantifiers
{ n }Matches the previous element exactly n times.
",\\d{3}" = >",043" in "1,043.6", ",876",",210" in "9,876,210"
Quantifiers
{ n ,}Matches the previous element one or more times.
"\d{2,}" = > "166", "29", "1930"
Quantifiers
{ n , m }Matches the previous element at least n times,
but no more than m times.
\d{3,5}" = > "166", "17668" , => "19302" in "193024"
Quantifiers
*?Matches the previous element zero or more times,
but as few times as possible.
\d*?\.\d => ".0", "19.9", "219.9"
Quantifiers
+?Matches the previous element one or more times,
but as few times as possible.
"be+?" = > "be" in "been", "be" in "bent"
Quantifiers
??Matches the previous element zero or one time,
but as few times as possible.
"rai??n" => "ran", "rain"
Quantifiers
{ n }?Matches the preceding element exactly n times.
\\",\\d{3}?" = > ",043" in "1,043.6", ",876", ",210" in "9,876,210"
Quantifiers
{ n ,}?Matches the previous element at least n times,
but as few times as possible.
"\\d{2,}?" = > "166", "29", "1930"
Quantifiers
{ n , m }?Matches the previous element between n and m times,
but as few times as possible.
\\d{3,5}?" => "166", "17668" , => "193", "024" in "193024"
Quantifiers
^Match must occur at the beginning of the string/line
^\\d{3} = > "901-" in "901-333-"
Anchors
$Match must occur at the end of the string/line or
before \n at the end of the string/line.
-\\d{3}$ = > "-333" in "901-333"
Anchors
\\AMatch must occur at the beginning of the string/line
(no multiline support) \\A\\d{3} = > "901" in "901-333"
Anchors
\\ZMatch must occur at the end of the string or before \n
at the end of the string -\\d{3}\\Z = > "901" in "901-333"
Anchors
\\zMatch must occur at the end of the string only
-\\d{3}\\z = > "-333" in "901-333"
Anchors
\\GBegin where the last match ended
\\G\(\\d\\) => "(1)", "(3)", "(5)" in "(1)(3)(5)[7](9)"
Anchors
\\bMatch must occur on a word boundary
\\b\\w+\\s\\w+\\b => "them them" in "them theme them them"
Anchors
\\BMatch must not occur on a word boundary
Bend\\w*\\b = > "ends", "ender" in "end sends endure lender"
Anchors
\\wMatch any word characters
"I", "D", "A", "1", "3" in "ID A1.3"
Character Classes
\\WMatch any non word characters
" ", "." in "ID A1.3"
Character Classes
\\w+Match one or more word characters
\\w*Match zero or more word characters
\\sMatch any white space character
"D " in "ID A1.3"
Character Classes
\\SMatch any non white space character
" _" in "int __ctr"
Character Classes
\\s?Match zero or one space
\\s*Match zero or more space
\\dMatch any decimal digit
"4" in "4 = IV"
Character Classes
\\DMatch any character not decimal digit
" ", "=", " ", "I", "V" in "4 = IV"
Character Classes
[0-9]+Match one or more decimal digits
[0-9]{0,3}Match 0 to 3 occurrence of the decimal digits 0 through 9
[-+]?Match zero or one of either a positive or negative sign

Pattern
SSN pattern: "\d{3}-\d{2}-\d{4}"
ZIP pattern: "\d{5}(-\d{4})?"
US Phone pattern: "(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}"
Date format pattern: "\\d{1,2}(/|-)\\d{1,2}\\1(\\d{4}|\\d{2})$"
Email pattern: "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"
URL pattern: "http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"
IPAddress pattern: "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$"


No comments: