copy with modif from: cheat sheet page 165 in book https://leanpub.com/bastards-regexes
| Syntax | Explanation | Example | ||||
|---|---|---|---|---|---|---|
| Find | Replace | Original text | Replace first | Replace all | ||
^ |
Beginning of line | ^a |
A |
an aria | An aria | (same) |
$ |
End of line | a$ |
d |
an aria | an arid | (same) |
\b |
Word boundary | \ba\b |
an |
an aria a | an aria | an aria an |
\B |
Not a word boundary | a\B |
o |
an aria | on oria | (same) |
\s |
Whitespace character (includes new lines) | \s |
- |
Hey hey hey | Hey-hey hey | Hey-hey-hey |
\S |
Not a whitespace character | \S |
- |
Hey hey | -ey hey | --- --- |
\n or \r |
Newline character | \n |
- |
Hey hey hey | Hey-hey hey | Hey-hey-hey |
\t |
Tab character | \t |
, |
Albany NY | Albany,NY US | Albany,NY,US |
. |
Match any character except newlines | a.. |
xyz |
an apple a bear | xyzapple a bear | xyzxyzle xyzear |
\w |
A word character: letters, numbers, including underscore | a\w |
xy |
an apple | xy apple | xy xyple |
\W |
Not a word character | \W |
- |
The cat. | The-cat. | The-cat- |
x|y |
Either x or y |
dog|cat |
rat |
The cat and dog | The rat and dog | The rat and rat |
[abc] |
Either of a, b, c |
[abc] |
x |
a bearcat | x bearcat | x bexrxxt |
[^xyz] |
Anything but x, y, z |
[^abc] |
x |
a bearcat | a bxarcat | a bxaxcax |
[a-z] |
Any letters a through z |
[A-Z] |
x |
The Cat | xhe Cat | xhe xat |
? |
Zero or one | x?ray |
laser |
xray ray xxray | laser ray xxray | laser laser xlaser |
* |
Zero or more | x*ray |
laser |
xray ray xxray | laser ray xxray | laser laser laser |
+ |
One or more | \w+a |
x |
baaaa baaaa | x baaaa | x x |
+? |
Lazy one or more | \w+?a |
x |
baaaa baaaa | xaaa baaaa | xxa xxa |
{2} |
Match exactly 2 occurrences | ba{2} |
x |
babaaaa babaaaa | baxaa babaaaa | baxaa baxaa |
{2,6} |
Match between 2 to 6 occurrences | ba{2,3} |
x |
babaaaa babaaaa | baxa babaaaa | baxa baxa |
{2,} |
Match 2 or more occurrences | ba{2,} |
x |
babaaaa babaaaa | bax babaaaa | bax bax |
a(?=bc) |
Positive lookahead | J(?=on) |
D |
Jon Jay Jones | Don Jay Jones | Don Jay Dones |
a(?!bc) |
Negative lookahead | J(?!on) |
D |
Jon Jay Jones | Jon Day Jones | (same) |
(?<=a)bc |
Positive look behind | (?<=\d)\.\d\d |
bucks |
He spent 12.42. | He spent 12 bucks. | (same) |
(?<!a)bc |
Negative look behind | (?<!\d)\. |
! |
He spent 12.42. | He spent 12.42! | (same) |
(?:abc) |
Non-capturing group | gr(?:a|e)y |
black |
grey and gray | black and gray | black and black |
(abc) |
Capturing group | (\d)(\d)(\d) |
\3\2\1 or $3$2$1 |
123456 | 321456 | 321654 |
\ |
Skip a metacharacter after the backslash | \? |
. |
He said it?? | He said it.? | He said it.. |
copied from:
| Character | Description |
|---|---|
\a |
Match a BELL \u0007. |
\A |
Match at the beginning of the input. Differs from ^ in that \A will not match after a new line within the input. |
\b |
Match if the current position is a word boundary. Boundaries occur at the transitions between word (\w) and non-word (\W) characters, with combining marks ignored. |
\B |
Match if the current position is not a word boundary. |
\cX |
Match a control-X character. |
\d |
Match any character with the Unicode General Category of Nd (Number, Decimal Digit.) |
\D |
Match any character that is not a decimal digit. |
\e |
Match an ESCAPE \u001B. |
\f |
Match a FORM FEED \u000C. |
\G |
Match if the current position is at the end of the previous match. |
\h |
Match a Horizontal White Space character. They are characters with Unicode General Category of Space_Separator plus the ASCII tab \u0009. |
\H |
Match a non-Horizontal White Space character. |
\k<name> |
Named Capture Back Reference. |
\n |
Match a LINE FEED \u000A. |
\N{UNICODE CHARACTER NAME} |
Match the named character. |
\p{UNICODE PROPERTY NAME} |
Match any character with the specified Unicode Property. |
\P{UNICODE PROPERTY NAME} |
Match any character not having the specified Unicode Property. |
\Q … \E |
Quotes all characters. |
\r |
Match a CARRIAGE RETURN \u000D. |
\R |
Match a new line character, or the sequence CR LF. The new line characters are \u000a, \u000b, \u000c, \u000d, \u0085, \u2028, \u2029. |
\s |
Match a white space character. White space is defined as [\t\n\f\r\p{Z}]. |
\S |
Match a non-white space character. |
\t |
Match a HORIZONTAL TABULATION \u0009. |
\uhhhh |
Match the character with the hex value hhhh. |
\Uhhhhhhhh |
Match the character with the hex value hhhhhhhh. Exactly 8 hex digits must be provided, even though the largest Unicode code point is \U0010ffff. |
\v |
Match a new line character. The new line characters are \u000a, \u000b, \u000c, \u000d, \u0085, \u2028, \u2029. Does not match the new line sequence CR LF. |
\V |
Match a non-new line character. |
\w |
Match a word character. Word characters are [\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\u200c\u200d]. |
\W |
Match a non-word character. |
\x{hhhh} |
Match the character with hex value hhhh. From 1 to 6 hex digits may be supplied. |
\xhh |
Match the character with two digit hex value hh. |
\X |
Match a Grapheme Cluster. |
\Z |
Match if the current position is at the end of input, but before the final line terminator, if one exists. |
\z |
Match if the current position is at the end of input. |
\n |
Back Reference. Match whatever the nth capturing group matched. n must be a number > 1 and < total number of capture groups in the pattern. |
\0ooo |
Match an Octal character. ‘ooo’ is from one to three octal digits. 0377 is the largest allowed Octal character. The leading zero is required; it distinguishes Octal constants from back references. |
[pattern] |
Match any one character from the set. |
. |
Match any character. |
^ |
Match at the beginning of a line. |
$ |
Match at the end of a line. Line terminating characters are \u000a, \u000b, \u000c, \u000d, \u0085, \u2028, \u2029 and the sequence \u000d \u000a. |
\ |
Quotes the following character. Characters that must be quoted to be treated as literals are * ? + [ ( ) { } ^ $ | \ . |
| Operator | Description |
|---|---|
| |
Alternation. A|B matches either A or B. |
* |
Match 0 or more times. Match as many times as possible. |
+ |
Match 1 or more times. Match as many times as possible. |
? |
Match zero or one times. Prefer one. |
{n} |
Match exactly n times |
{n,} |
Match at least n times. Match as many times as possible. |
{n,m} |
Match between n and m times. Match as many times as possible, but not more than m. |
*? |
Match 0 or more times. Match as few times as possible. |
+? |
Match 1 or more times. Match as few times as possible. |
?? |
Match zero or one times. Prefer zero. |
{n}? |
Match exactly n times. |
{n,}? |
Match at least n times, but no more than required for an overall pattern match. |
{n,m}? |
Match between n and m times. Match as few times as possible, but not less than n. |
*+ |
Match 0 or more times. Match as many times as possible when first encountered, do not retry with fewer even if overall match fails (Possessive Match). |
++ |
Match 1 or more times. Possessive match. |
?+ |
Match zero or one times. Possessive match. |
{n}+ |
Match exactly n times. |
{n,}+ |
Match at least n times. Possessive Match. |
{n,m}+ |
Match between n and m times. Possessive Match. |
(…) |
Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match. |
(?:…) |
Non-capturing parentheses. Groups the included pattern, but does not provide capturing of matching text. Somewhat more efficient than capturing parentheses. |
(?>…) |
Atomic-match parentheses. First match of the parenthesized subexpression is the only one tried; if it does not lead to an overall pattern match, back up the search for a match to a position before the (?>. |
(?#…) |
Free-format comment. |
(?=…) |
Look-ahead assertion. True if the parenthesized pattern matches at the current input position, but does not advance the input position. |
(?!…) |
Negative look-ahead assertion. True if the parenthesized pattern does not match at the current input position. Does not advance the input position. |
(?<=…) |
Look-behind assertion. True if the parenthesized pattern matches text preceding the current input position, with the last character of the match being the input character just before the current position. Does not alter the input position. The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators.) |
(?<!…) |
Negative Look-behind assertion. True if the parenthesized pattern does not match text preceding the current input position, with the last character of the match being the input character just before the current position. Does not alter the input position. The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators.) |
(?<name>…) |
Named capture group. The are literal - they appear in the pattern. |
(?ismwx-ismwx:…) |
Flag settings. Evaluate the parenthesized expression with the specified flags enabled or -disabled. |
(?ismwx-ismwx) |
Flag settings. Change the flag settings. Changes apply to the portion of the pattern following the setting. For example, (?i) changes to a case insensitive match. |
| Flag (pattern) | Description |
|---|---|
i |
If set, matching will take place in a case-insensitive manner. |
x |
If set, allow use of white space and #comments within patterns. |
s |
If set, a “.” in a pattern will match a line terminator in the input text. By default, it will not. Note that a carriage-return / line-feed pair in text behave as a single line terminator, and will match a single “.” in a RE pattern. Line terminators are \u000a, \u000b, \u000c, \u000d, \u0085, \u2028, \u2029 and the sequence \u000d \u000a. |
m |
Control the behavior of “^” and “$” in a pattern. By default these will only match at the start and end, respectively, of the input text. If this flag is set, “^” and “$” will also match at the start and end of each line within the input text. |
w |
Controls the behavior of \b in a pattern. If set, word boundaries are found according to the definitions of word found in Unicode UAX 29, Text Boundaries. By default, word boundaries are identified by means of a simple classification of characters as either “word” or “non-word”, which approximates traditional regular expression behavior. The results obtained with the two options can be quite different in runs of spaces and other non-word characters. |
| Character | Descriptions |
|---|---|
$n |
The text of capture group ‘n’ will be substituted for $n. n must be >= 0 and not greater than the number of capture groups. An unescaped $ in replacement text that is not followed by a capture group specification, either a number or name, is an error. |
${name} |
The text of named capture group will be substituted. The name must appear in the pattern. |
\ |
Treat the following character as a literal, suppressing any special meaning. Backslash escaping in substitution text is only required for ‘$’ and ‘\’, but may be used on any other character without bad effects. |
| Example | Description |
|---|---|
[abc] |
Match any of the characters a, b or c. |
[^abc] |
Negation - match any character except a, b or c. |
[A-M] |
Range - match any character from A to M. The characters to include are determined by Unicode code point ordering. |
[\u0000-\U0010ffff] |
Range - match all characters. |
[\p{L}] |
Characters with Unicode Category = Letter. All forms shown are equivalent. |
[\P{Letter}] |
Negated property. (Upper case \P) Match everything except Letters. |
[\p{numeric_value=9}] |
Match all numbers with a numeric value of 9. Any Unicode Property may be used in set expressions. |
[[a-z][A-Z][0-9]] |
Logical OR or Union of Sets. The examples match ASCII letters and digits. The two forms are equivalent. |
[\p{Letter}&&\p{script=cyrillic}] |
Logical AND or intersection. Match the set of all Cyrillic letters. |
[\p{Letter}--\p{script=latin}] |
Subtraction. Match all non-Latin letters. |
[\p{letter}~~\p{ascii}] |
Logical XOR or symmetric difference. Match either letter or ascii, but not both |
| Positive | Negative | |
|---|---|---|
| POSIX-style Syntax | [:type=value:] |
[:^type=value:] |
| Perl-style Syntax | \p{type=value} |
\P{type=value} |
Full list: https://www.unicode.org/Public/UCD/latest/ucd/PropertyValueAliases.txt