Thursday, January 22, 2009

Regular Expression Tips

JavaScript Tester: http://codespec.blogspot.com/2007/10/regular-expression-tips.html

foob.*r
: matchs strings like 'foobar', 'foobalkjdflkj9r' and 'foobr'
foob.+r : matchs strings like 'foobar', 'foobalkjdflkj9r' but not 'foobr'
foob.?r : matchs strings like 'foobar', 'foobbr' and 'foobr' but not 'foobalkj9r'
fooba{2}r : matchs the string 'foobaar'
fooba{2,}r : matchs strings like 'foobaar', 'foobaaar', 'foobaaaar' etc.
fooba{2,3}r : matchs strings like 'foobaar', or 'foobaaar' but not 'foobaaaar'

Metacharacters \1 through \9 are interpreted as backreferences. \ matches previously matched subexpression #.

Examples:

(.)\1+ matchs 'aaaa' and 'cc'.
(.+)\1+ also match 'abab' and '123123'
(['"]?)(\d+)\1 matchs '"13" (in double quotes), or '4' (in single quotes) or 77 (without quotes) etc

Metacharacters - line separators

^ start of line
$ end of line
\A start of text
\Z end of text
. any character in line

Examples:

^foobar matchs string 'foobar' only if it's at the beginning of line
foobar$ matchs string 'foobar' only if it's at the end of line
\^foobar$ matchs string 'foobar' only if it's the only string in line
\foob.r matchs strings like 'foobar', 'foobbr', 'foob1r' and so on


Metacharacters - predefined classes

\w an alphanumeric character (including "_")
\W a nonalphanumeric
\d a numeric character
\D a non-numeric
\s any space (same as [ \t\n\r\f])
\S a non space

You may use \w, \d and \s within custom character classes .

Examples:

\foob\dr matchs strings like 'foob1r', ''foob6r' and so on but not 'foobar', 'foobbr' and so on
\foob[\w\s]r matchs strings like 'foobar', 'foob r', 'foobbr' and so on but not 'foob1r', 'foob=r' and so on

No comments: