/[a-z]/ |
a thru z |
/[a-zA-Z]/ |
a thru z and A thru Z |
/[^a]/ |
exclude a |
/(\w)/ |
group |
/(\w)\1/ |
reuses (\w) |
Regular Expressions Cheat Sheet
A quick reference guide for regular expressions (regex), including symbols, ranges, grouping, assertions and some sample patterns to get you started.
Regular Expressions General
Character Classes & Groups
Flags
/regex/flag |
structure |
i |
ignores case |
g |
global, searches multiple times |
Patterns
/regex/ |
litteral match |
/regex|moreregex/ |
Literal match w/ different possibilities |
/?/ |
lazy search |
/^a/ |
returns a if first |
/a$/ |
returns a if last |
/(?=a)/ |
returns if a is present |
/(?!a)/ |
returns if a is NOT present |
/./ |
wildcard character |
/a?/ |
a optional |
/[a+]/ |
a or aa as single match (1 or more) |
/[Aa*]/ |
Aa or A as single matches (0 or more) |
/[A-z]{a,b}/ |
min a, max b |
/[A-z]{x,}/ |
min x times |
/[A-z]{x}/ |
only x times |
/[A-z],{x}/ |
max x times |
Methods
regex.test(string) |
returns bool |
string.match(regex) |
returns regex from string w/ extract, index and input |
string.replace(regex, input) |
replaces regex in string with input (can be function or string) |
string.replace(regex, '$a $b') |
a & b can be capture groups from regex |
Working with URL
|
utility to work with URL |
|
|
display page content in the file |
|
|
display page content in the file, with server file name |
|
example |
||
|
perform GET request |
|
example |
|
DB access
|
connect to the DB (utility) |
|
|
host of DB |
|
|
user for DB login |
|
example |
|
Global search
|
|
Globally search for a REgular expression and Print matching lines |
|
registerless search |
|
|
full word match |
|
|
check number of matches |
|
|
search in subdirectory |
|
|
inversion search |
|
|
enumerate lines |
|
|
display names |
|
`-e [pattern] [file] |
several search patterns |
|
example |
|
Integration test
|
service avialability |
|
|
check ports |
|
example |
||
|
Reading logs
|
|
shows the end of file |
|
display desired (300) number of rows |
|
|
display real time file updates |
|
example |
|
display 300 rows from the bottom |
|
shows the top of file |
Asserções
?= |
Lookahead assertion |
?! |
Negative lookahead |
?<= |
Lookbehind assertion |
?!= or ?<! |
Negative lookbehind |
?> |
Once-only Subexpression |
?() |
Condition [if then] |
?()| |
Condition [if then else] |
?# |
Comment |
Âncoras
^ |
Início da string ou início da string com várias linhas |
\A |
início da string |
$ |
Fim da string ou fim da string com várias linhas |
\Z |
Fim da string |
\b |
Limite da palavra |
\B |
Sem limite da palavra |
\< |
Início da palavra |
\> |
Fim da palavra |
Posix Classes
[:xdigit:] |
Hexadecimal digits |
[:upper:] |
Uppercase letters |
[:lower:] |
Lowercase letters |
[:alpha:] |
Letters |
[:alnum:] |
Letters and digits |
[:ascii:] |
Ascii codes 0 - 127 |
[:cntrl:] |
Control characters |
[:print:] |
Visible characters |
[:punct:] |
Visible punctuation characters |
Special Character Classes
\\ |
General Escape |
\n |
New line or line feed |
\r |
Carriage return (used to reset a device's position to the beginning of a line of text) |
\t |
Tab |
\0 |
Null character |
\xHH |
Hexademical digit HH |
\oOOO |
Octal digit OOO |
[\b] |
Backspace (yes it's inside brackets) |
\f |
Form feed (next page) |
\v |
Vertical tab |
\Q...\E |
Remove special meaning from a sequence of characters |
Character Classes and Ranges
. |
Any character except new line (\n) |
[abc-f] |
One character of: a, b, c, d, e, f |
[^abc-f] |
One character except: a, b, c, d, e, f |
\d |
One digit |
\D |
One non-digit |
\s |
One whitespace |
\S |
One non-whitespace |
\w |
One word [a-zA-Z0-9_] |
\W |
One non-word [^a-zA-Z0-9_] |
Groups
(...) |
Capturing |
(?P<obj>...) |
Capturing group named "obj" |
(?P=obj) |
Match the named group "obj" |
\g{Z} |
Match the named or numbered group Z |
\Y |
Match the Y'th captured group |
(?>...) |
Atomic group: This group does not allow any backtracking to occur |
(?|...) |
Duplicate group numbers |
(?#...) |
Comment between parenthesis |
(?:...) |
Matches but does not capture the group |
(?&obj) |
Recurse into the named group "obj" |
(?Y) |
Recurse into numbered group Y |
(?R) |
Recurse into entire pattern |
\g<Z> |
Recurse into the named or numbered group Z |
Flags or Modifiers
g |
Global match |
i |
Ignore case |
m |
Multiple lines (^ and $ match start and end of a line) |
s |
Single line (. matches newline) |
x |
Specifies that whitespace characters in the pattern are ignored except when escaped or inside a character class |
J |
Duplicate group names allowed |
U |
Ungreedy quantifiers |
A |
Anchored, the pattern is forced to match only at the beginning (^) |
E |
Anchored, the pattern is forced to match only at the end ($) |
String Replacement
$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 |
Some regex implementations use \ instead of $.
Pattern Modifiers
g |
Global match |
i * |
Case-insensitive |
m * |
Multiple lines |
s * |
Treat string as single line |
x * |
Allow comments and whitespace in pattern |
e * |
Evaluate replacement |
U * |
Ungreedy pattern |
* PCRE modifier
Groups and Ranges
. |
Any character except new line (\n) |
(a|b) |
a or b |
(...) |
Group |
(?:...) |
Passive (non-capturing) group |
[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" |
Ranges are inclusive.
Special Characters
\n |
New line |
\r |
Carriage return |
\t |
Tab |
\v |
Vertical tab |
\f |
Form feed |
\xxx |
Octal character xxx |
\xhh |
Hex character hh |
Common Metacharacters
^ |
[ |
. |
$ |
{ |
* |
( |
\ |
+ |
) |
| |
? |
< |
> |
The escape character is usually \
Escape Sequences
\ |
Escape following character |
\Q |
Begin literal sequence |
\E |
End literal sequence |
"Escaping" is a way of treating characters which have a special meaning in regular expressions literally, rather than as special characters.
Quantifiers
* |
0 or more |
{3} |
Exactly 3 |
+ |
1 or more |
{3,} |
3 or more |
? |
0 or 1 |
{3,5} |
3, 4 or 5 |
Add a ? to a quantifier to make it ungreedy.
Assertions
?= |
Lookahead assertion |
?! |
Negative lookahead |
?<= |
Lookbehind assertion |
?!= or ?<! |
Negative lookbehind |
?> |
Once-only Subexpression |
?() |
Condition [if then] |
?()| |
Condition [if then else] |
?# |
Comment |
POSIX
[:upper:] |
Upper case letters |
[:lower:] |
Lower case letters |
[:alpha:] |
All letters |
[:alnum:] |
Digits and letters |
[:digit:] |
Digits |
[:xdigit:] |
Hexadecimal digits |
[:punct:] |
Punctuation |
[:blank:] |
Space and tab |
[:space:] |
Blank characters |
[:cntrl:] |
Control characters |
[:graph:] |
Printed characters |
[:print:] |
Printed characters and spaces |
[:word:] |
Digits, letters and underscore |
Character Classes
\c |
Control character |
\s |
White space |
\S |
Not white space |
\d |
Digit |
\D |
Not digit |
\w |
Word |
\W |
Not word |
\x |
Hexadecimal digit |
\O |
Octal digit |
Anchors
^ |
Start of string, or start of line in multi-line pattern |
\A |
Start of string |
$ |
End of string, or end of line in multi-line pattern |
\Z |
End of string |
\b |
Word boundary |
\B |
Not word boundary |
\< |
Start of word |
\> |
End of word |