Regex
Cheat sheet for using regular expression
Syntax
\ Escaping
make a metacharacter represents itself
[] (|) OR operation
match any character inside the []
Except ^
[^a-z] matches any character except lowercase letter a-z
[^a-z0-9] matches any character except lowercase letter a-z and digits
* + {} ? Quantifiers
* zero or more
+ one or more
{} range
? either have it or not
. Wildcard
anything
[a-zA-Z0-9] Range
\d \w \s Character Classes
\d any number
\D anything except for number
\w any alphabet or number
\W anything except for number and alphabets
\s any whitespace: \t tab \r return \n newline vertical tab and form feed character
\S anything except for whitespace
^ $ Anchors
^ line start
$ line end
Add-on
Grouping and Capturing
() group the results that match the criteria between the brackets
(?:expression) non-captured groups
(?<group_name>regular_expression) OR (?'named_group'regular_expression) define explicit names
\b Boundary
\1 \k<name> Back Referencing
\1 for automatic numbered group
\k<name> OR \k'name' for explicit named group
Greedy and Lazy Quantifiers
Greedy Quantifiers(default): searches for the longest string
? Lazy Quantifiers: searches for the shortest string (more costly)
+-------------------+-----------------+----------------------------+
| Greedy quantifier | Lazy quantifier | Description |
+-------------------+-----------------+----------------------------+
| * | *? | Star Quantifier: 0 or more |
| + | +? | Plus Quantifier: 1 or more |
| ? | ?? | Optional Quantifier: 0 or 1|
| {n} | {n}? | Quantifier: exactly n |
| {n,} | {n,}? | Quantifier: n or more |
| {n,m} | {n,m}? | Quantifier: between n and m|
+-------------------+-----------------+----------------------------+