expression | description |
---|---|
. | Any character except newline |
a | The character a |
ab | The string ab |
a | b |
a* | 0 or more a's |
\ | Escapes a special character |
##RegEx escape sequences "Escaping" is a way of treating characters which have a special meaning in regular expressions literally, rather than as special characters.
expression | description |
---|---|
\ | escape the following character |
\Q | begin literal sequence |
\E | end literal sequence |
expression | description |
---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{2} | Exactly 2 |
{2, 5} | Between 2 and 5 |
2, | 2 or more |
Note: default is greedy. Append ? for reluctant
expression | description |
---|---|
(...) | capturing group |
(?:...) | passive non-capturing group |
\Y | Match the Y'th captured group |
. | Any character except new line (\n) |
(a | b) |
[abc] | Range (a or b or c) |
[^abc] | Not (a or b or c) |
[a-q] | Lower case letter from a to q |
[A-Q] | Upper case letter from A to Q |
[0-7] | Digit from 0 to 7 |
\x | Group/subpattern number "x" |
Note: Ranges are inclusive
expression | description |
---|---|
[ab-d] | One character of: a, b, c, d |
[^ab-d] | One character except: a, b, c, d |
[\b] | backspace character |
\d | One digit |
\D | One non-digit |
\O | octal digit |
\s | One whitespace |
\S | One non-white-space |
\w | One word character |
\W | One non-word character |
\x | hexadecimal digit |
##RegEx Anchors
expression | description |
---|---|
^ | start of string |
$ | end of string |
\b | word boundary |
\B | non-word boundary |
< | start of word |
> | end of word |
##RegEx Flags (pattern modifiers)
expression | description |
---|---|
g | global match |
i | ignore case |
i* | case-sensitive |
m | ^ and $ match start and end of line |
m* | multiple lines |
s* | treat string as single line |
x* | allow comments and whitespace pattern |
e* | evaluate placement |
U* | ungreedy pattern |
##RegEx special characters
expression | description |
---|---|
\n | Newline |
\r | Carriage return |
\t | Tab |
\0 | null character |
\YYY | Octal character YYY |
\xYY | Hexadecimal character YY |
\uYYYY | Hexadecimal character YYYY |
\cY | control character |
##RegEx replacement
expression | description |
---|---|
$$ | inserts $ |
$& | insert entire match |
$` | insert preceding string |
$' | insert the following string |
$Y | insert Y'th captured group |
$n | nth non-passive group |
$2 | "xyz" in /^(abc(xyz))$/ |
$1 | "xyz" in /^(?:abc)(xyz)$/ |
$` | Before matched string |
$' | After matched string |
$+ | Last matched string |
$& | Entire matched string |
Note: Some regex implementations use \ instead of $.
##RegEx Assertions
expression | description |
---|---|
?= | lookahead assertion |
?! | negative lookahead |
?<= | lookbehind assertion |
?!= or ?<! | negative lookbehind |
?> | Once-only subexpression |
?() | condition, [if then] |
?() | (add pipe to end) condition, [if then else]* |
?# | comment |
*: should be
?()|
but wont print to table in markdown
How to replace a all underscored string into spaces