
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

Each symbol matches a single character:
. anything 1 \d digit in 0123456789 \D non-digit \w āwordā (letters and digits and _) \W non-word ⣠space \t tab \r return \n new line 2 \s whitespace (ā£, \t, \r, \n) \S non-whitespace
Character classes [...] match any of the charac- ters in the class. Ex: [aeiou] matches vowels. Use Ė to specify the complement set: [Ėaeiou] matches non-vowels (including non-letters!). Use - to specify a range of letters: [a-e] matches abcde and [0-9a-f] matches ā0123456789abcdefā.
Boundary characters are helpful in āanchor- ingā your pattern to some edge, but do not select any characters themselves. \b word boundaries (defined as any edge between a \w and \W) \B non-word-boundaries Ė the beginning of the line $ the end of the line Ex: \bcat \b finds a match in āthe cat in the hatā but not in ālocateā.
(X|Y) X or Y Ex: \b(cat|dog)s\b matches cats and dogs.
X* 0 or more repetitions of X X+ 1 or more repetitions of X X? 0 or 1 instances of X X{m} exactly m instances of X X{m,} at least m instances of X X{m,n} between m and n (inclusive) in- stances of X By default, quantifiers just apply to the one character. Use (...) to specify explicit quanti- fier āscope.ā Ex: ab+ matches ab,abb,abbb,abbbb... (ab)+ matches ab,abab,ababab... Quantifiers are by default greedy in regex. Good regex engines support adding? to a quantifier to make it lazy. Ex: greedy: ^.b aabaaba lazy: ^.?b aabaaba
The characters {}^$.|*+?\ (and - inside [...]) have special meaning in regex, so they must be āescapedā with \ to match them. Ex: . matches the period. and \ matches the backslash .
Count your open parentheses ( from the left, starting with 1. Whatever is matched by paren- thesis number n can be referenced later by \n. Ex: \b(\w+)ā£\1\b matches two identical words with a space in between Backreferences are useful for find/replace s: Ex: Finding \b (\w +)er\b and replacing with more \1 will map āthe taller manā 7 ā āthe more tall manā and āI am shorterā 7 ā āI am more shortā.
Read about ānon-capturing parenthesesā and ālook-aheadā and ālook-behindā online. Also, visualize your regexes as finite-state machines at http://www.regexper.com/. 1 ...except line breaks, depending on your engine. 2 Depending on where you got your file, line breaks may be \r, \n, or \r\n. Also, in some regex engines (e.g. TextWrangler), \r and \n match the same things.
Hadas Kotek (courtesy of Michael Y. Erlewine)