Regex Guide
A quick reference guide to common regex characters
Regex Search Modifiers
- |
marks an alternative acceptable text
/(?:sample)|(?:other)/
- {3}
requires text to repeat specified amount of times
/(?:sample){5}/
- {3,}
requires text to repeat at least the specified amount of times
/(?:sample){5, }/
- {3, 6}
requires text to repeat between the minimum and max range specified
/(?:sample){5, 8}/
Marks text as occuring once or more, using a greedy search
/(?:sample)+/
Marks text as occuring once or more, using a lazy search
/(?:sample)+?/
Marks text as occuring zero or more times, using a greedy search
/(?:sample)*/
Marks text as occuring zero or more times, using a lazy search
/(?:sample)*?/
- (?=...)
requires search text to be followed by specified text
/sample(?=after)/
- (?!...)
requires search text to NOT be followed by specified text
/sample(?!after)/
- (?<=...)
requires search text to be preceded by specified text
/(?<=pre)sample/
- (?<!...)
requires search text to NOT be preceded by specified text
/(?<!pre)sample/
marks text as occuring at beginning
/^(?:sample)/
marks text as occuring at end
/(?:sample)$/
marks text as optional
/(?:sample)?/
- (...)
encapsulates text as a group and captures when matching
/(sample)/
- (?:...)non-capture grouping
encapsulates text as a group without capturing
/(?:sample)/
- (?<...>...) ... \k<...>
uses a named capture group to match each \k<...> to the first instance
/(?<var>)\d{2}) and \k<var>/
Symbol | Name | Description | Example |
---|---|---|---|
| | marks an alternative acceptable text | (?:sample)|(?:other) | |
{3} | requires text to repeat specified amount of times | (?:sample){5} | |
{3,} | requires text to repeat at least the specified amount of times | (?:sample){5, } | |
{3, 6} | requires text to repeat between the minimum and max range specified | (?:sample){5, 8} | |
+ | Marks text as occuring once or more, using a greedy search | (?:sample)+ | |
+? | Marks text as occuring once or more, using a lazy search | (?:sample)+? | |
* | Marks text as occuring zero or more times, using a greedy search | (?:sample)* | |
*? | Marks text as occuring zero or more times, using a lazy search | (?:sample)*? | |
(?=...) | requires search text to be followed by specified text | sample(?=after) | |
(?!...) | requires search text to NOT be followed by specified text | sample(?!after) | |
(?<=...) | requires search text to be preceded by specified text | (?<=pre)sample | |
(?<!...) | requires search text to NOT be preceded by specified text | (?<!pre)sample | |
^ | marks text as occuring at beginning | ^(?:sample) | |
$ | marks text as occuring at end | (?:sample)$ | |
? | marks text as optional | (?:sample)? | |
(...) | encapsulates text as a group and captures when matching | (sample) | |
(?:...) | non-capture grouping | encapsulates text as a group without capturing | (?:sample) |
(?<...>...) ... \k<...> | uses a named capture group to match each \k<...> to the first instance | (?<var>)\d{2}) and \k<var> |
Special Characters
matches any single character
- [^...]
matches any single character not listed between [^ and ]
matches any single digit
- \Dnon-digit
matches any single character that is not a digit
matches any single letter
- \Wnon-letter
matches any single character that is not a letter
- \sspace
matches a space between characters such as " "
marks a text as having a boundary, such as a space or start of the text
Symbol | Name | Description |
---|---|---|
. | matches any single character | |
[^...] | matches any single character not listed between [^ and ] | |
\d | matches any single digit | |
\D | non-digit | matches any single character that is not a digit |
\w | matches any single letter | |
\W | non-letter | matches any single character that is not a letter |
\s | space | matches a space between characters such as " " |
\b | marks a text as having a boundary, such as a space or start of the text |