Docsity

PREFACE, Schemes and Mind Maps of English Literature

Toggle sidebar
bg1
Regular Expressions cheat sheet
Basic matching
Each symbol matches a single character:
.anything1
\d digit in 0123456789
\D non-digit
\w ā€œwordā€ (letters and digits and _)
\W non-word
␣ space
\t tab
\r return
\n new line2
\s whitespace (␣, \t,\r,\n)
\S non-whitespace
Character classes
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ā€˜.
Boundaries
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ā€.
Disjunction
(X|Y) X or Y
Ex: \b(cat|dog)s\b matches cats and dogs.
ā€œQuantifiersā€
X* 0 or more repetitions of X
X+ 1 or more repetitions of X
X? 0 or 1 instances of X
X{m}exactly minstances of X
X{m,} at least minstances of X
X{m,n}between mand 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
Special characters
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 \.
Backreferences
Count your open parentheses (from the left,
starting with 1. Whatever is matched byparen-
thesis number ncan be referenced later by \n.
Ex: \b(\w+)␣\1\b matches two identical words
with a space in between
Backreferences are useful for find/replaces:
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ā€.
Advanced
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.
2Depending 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)

Partial preview of the text

Download PREFACE and more Schemes and Mind Maps English Literature in PDF only on Docsity!

Regular Expressions cheat sheet

Basic matching

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

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ā€˜.

Boundaries

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ā€.

Disjunction

(X|Y) X or Y Ex: \b(cat|dog)s\b matches cats and dogs.

ā€œQuantifiersā€

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

Special characters

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 .

Backreferences

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ā€.

Advanced

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)