Overview
Basics of regular expressions. This summarizes commonly used notations in ERE (Extended Regular Expression).
Character Classes
[string]
- Matches any single character in the string.
- Ex:
- [fox]
- fox raccoon cat
- f, o, x
- fox raccoon cat
- [fox]
[^string]
- Matches any single character not in the string.
- Ex:
- [^fox]
- fox raccoon cat
- r, a, c, o, n
- fox raccoon cat
- [^fox]
[string-string]
- Matches any single character in the specified range.
- Ex:
- [a-z]
- fox raccoon cat
- f, o, x, r, a, c, o, n
- fox raccoon cat
- [a-z]
\d
- Matches decimal digits.
- Ex:
- \d
- There are 10 apples
- 1, 0
- There are 10 apples
- \d
\D
- Matches any character that is not a decimal digit.
- Ex:
- \D
- There are 10 apples
- T, h, e, r, e, a, p, l, e, s
- There are 10 apples
- \D
\w
- Matches all alphanumeric characters and underscores.
- Ex:
- \w
- abc_*
- a, b, c, _
- abc_*
- \w
\W
- Matches all characters that are not alphanumeric or underscores.
- Ex:
- \W
- abc_*
-
- abc_*
- \W
\s
- Matches whitespace characters.
- Ex:
- \s
- a b c
- Matches two whitespace characters (between a and b, and between b and c)
- a b c
- \s
\S
- Matches any character that is not a whitespace.
- Ex:
- \S
- a b c
- a, b, c
- a b c
- \S
Anchors
^
- Matches the beginning of a line.
- Ex:
- ^Thank you
- Thank you, my friend
- Yesterday, thank you ✗
- ^Thank you
$
- Matches the end of a line.
- Ex:
- thank you$
- Thank you, my friend ✗
- Yesterday, thank you ○
- thank you$
Grouping Constructs
(subexpression)
- Captures the string that matches the subexpression.
- Ex:
- (ri){2}
- aririri thank you ○
- ariri thank you ✗
- (ri){2}
Quantifiers
*
- Matches when the preceding element is repeated 0 or more times.
- Greedy quantifier.
- Ex:
- ab*
- ab
- ab
- abab
- ab, ab
- aabb
- ab
- abbb
- abbb
- a
- a
- ba
- a
- ab
- ab*
+
- Matches when the preceding element is repeated 1 or more times.
- Greedy quantifier.
- Ex:
- ab+
- ab
- ab
- abab
- ab, ab
- aabb
- abb
- abbb
- abbb
- a
- No match
- ba
- No match
- ab
- ab+
?
- Matches when the preceding element is repeated 0 or 1 time.
- Greedy quantifier.
- Ex:
- ab?
- ab
- ab
- abab
- ab, ab
- aabb
- a, ab
- abbb
- ab
- a
- a
- ba
- a
- ab
- ab?
*?
- Matches when the preceding element is repeated 0 or more times.
- Lazy quantifier.
- Ex:
- ab*?
- ab
- a
- abab
- a, a
- aabb
- a, a
- abbb
- a
- a
- a
- ba
- a
- ab
- ab*?
+?
- Matches when the preceding element is repeated 1 or more times.
- Lazy quantifier.
- Ex:
- ab+?
- ab
- ab
- abab
- ab, ab
- aabb
- ab
- abbb
- ab
- a
- No match
- ba
- No match
- ab
- ab+?
??
- Matches when the preceding element is repeated 0 or 1 time.
- Lazy quantifier.
- Ex:
- ab??
- ab
- a
- abab
- a, a
- aabb
- a, a
- abbb
- a
- a
- a
- ba
- a
- ab
- ab??
{n}
- Matches when the preceding element is repeated n times.
- Ex:
- b{2}
- abba
- bb
- abba
- b{2}
{n,}
- Matches when the preceding element is repeated n or more times.
- Ex:
- b{2,}
- abbba
- bbb
- abbba
- b{2,}
{n, m}
- Matches when the preceding element is repeated between n and m times.
- Ex:
- b{2,4}
- abbba
- bbb
- abbbba
- bbbb
- abbba
- b{2,4}
Alternation Constructs
|
- Matches any one of the separated strings.
- Ex:
- ab|cd
- abcd
- ab, cd
- aaccd
- cd
- abcd
- ab|cd