Regular Expressions Notes
Regular expressions (regex) let you search for a particular pattern. Regex consist of literal characters, and special characters, also called meta characters. Regex syntax has 2 modes, one inside of character classes, which is inside [ ] and one outsided of character classes.
Character classes []:
Regex inside [] are in different mode and have different syntax. It matches a
single character specified as a list, range or class shortcut.
03[-./]19[-./]76
Will match the above date delimited by a dot or a hyphen.
Inside [] the dot is no longer a meta-character, its matched literally!
Negation Operator ^
f[^u]
matches something with f, followed by any character other then u
Character classes shortcuts
d [0-9] digit
D non digit
w [a-zA-Z0-9_] part of word
W non word
s [ tnrfv] whitespace
S non whitespace
Alternation:
| operator is 'or'
Jeff(re|er|izz)y
=> Jeffrey, Jeffery, Jeffizzy
Conditionals:
(?if then | else)
if the "if" part is true, "then" expression is attmpted, otherwise
"else" is attempted
(?if then)
else condition is optional, so is the pipe character prefixing it
Anchors:
^ beg of line
$ end of line
b word boundary
B non-word boundary
Ruby:
A begining of a string
Z end of string (or before newline at the end)
z end of string
Optional Items:
? optionally matches a character before it, or you can specify a group() of
characters before it:
colou?r
will match color and colour
4(th)?
will match 4 and 4th
Repetitions:
+ s1 or more times
* 0 or more times
? 1 or 0 times
{n} match exactly n times
{min, max} min to max times
{min,} at least min
{,max} at most max (ruby)
Backreferences:
First matched group ():
sed, vi: 1
ruby: $1
php: $m[1]
python, java: m.group(1)
c#: m.Groups[1]
Group zero is typically the entire match
Escape:
. will escape dot meta character
More Info:
http://www.regular-expressions.info/



April 10th, 2008 at 10:21 pm
I have never seen anything that would look so disgusting. Why do people publish stuff like that? I don’t understand them!
August 30th, 2009 at 12:16 pm
I never ever post but this time I will,Thanks alot for the great blog.
February 3rd, 2010 at 11:54 pm
Glad to see that this site works well on my Blackberry Bold, everything I want to do is functional. Thanks for keeping it up to date with the latest.