Regex
- Overview
- Detailed
Regex - is a sequence of characters that define a search pattern.
- Anchors
- Character Classes
- POSIX
- Assertions
- Quantifiers
- Escape Sequences
- Special Characters
- Groups and Ranges
- Pattern Modifiers
- String Replacement
Regex Component | Meaning | Example |
---|---|---|
^ | Start of string, or start of line in multi-line pattern | ^cat cat, cats |
\A | Start of string | \Adog dog, dogs |
$ | End of string, or end of line in multi-line pattern | bird$ lovebird, tweeting bird, bird |
\Z | End of string | fish\Z goldfish, jellyfish, fish |
\b | Word boundary | \bwolf\b The wolf howls, Lone wolf |
\B | Not word boundary | \Bcat\ concatenate, scatter |
\< | Start of word | \<day day, days are long |
\> | End of word | night\> goodnight, tonight |
Regex Component | Meaning | Example |
---|---|---|
\c | Control character | \c \cC, \cZ |
\s | White space | \s \t, \n |
\S | Not white space | \S c, a |
\d | Digit | \d 0, 1 |
\D | Not digit | \D c, a |
\w | Word (alphanumeric or underscore) | \w a, _ |
\W | Not word | \W -, . |
\x | Hexadecimal digit | \x 0x0, 0xFF |
\O | Octal digit | \O 07, 075 |
Regex Component | Meaning | Example |
---|---|---|
[:upper:] | Upper case letters | [:upper:] A, B |
[:lower:] | Lower case letters | [:lower:] a, b |
[:alpha:] | All letters | [:alpha:] A, b |
[:alnum:] | Digits and letters | [:alnum:] 0, a |
[:digit:] | Digits | [:digit:] 0, 1 |
[:xdigit:] | Hexadecimal digits | [:xdigit:] 0, F |
[:punct:] | Punctuation | [:punct:] !, . |
[:blank:] | Space and tab | [:blank:] \t, " " |
[:space:] | Blank characters | [:space:] \t, \n |
[:cntrl:] | Control characters | [:cntrl:] \t, \n |
[:graph:] | Printed characters (excluding space) | [:graph:] A, ? |
[:print:] | Printed characters and spaces | [:print:] A, ? |
[:word:] | Digits, letters and underscore | [:word:] a, _ |
Regex Component | Meaning | Example |
---|---|---|
?= | Lookahead assertion | cat(?=s) cats, catalog |
?! | Negative lookahead | cat(?!s) cat, catch |
?<= | Lookbehind assertion | (?<=re)cat recat, precative |
?!= or ?<! | Negative lookbehind | (?<!re)cat cat, catch |
?> | Once-only Subexpression | (?>foo|fo)bar foobar, fofoobar |
?() | Condition (if-then) | (?(?=d)foo|bar) dog, food |
?()| | Condition (if-then-else) | (?(?=d)foo|bar) dog, food |
Add ?
to a quantifier to make it ungreedy.
Regex Component | Meaning | Example |
---|---|---|
* | 0 or more | go*gle ggle, google |
+ | 1 or more | go+gle google, gogle |
? | 0 or 1 | colou?r color, colour |
{3} | Exactly 3 | go{3}gle gooogle |
{3,} | 3 or more | go{3,}gle goooogle, goooogle |
{3,5} | 3, 4 or 5 | go{3,5}gle goooogle, goooooogle |
Escaping is a way of treating characters which have a special meaning in regular expressions literally, rather than as special characters.
Regex Component | Meaning | Example |
---|---|---|
\ | Escape following character | \$ The price is $10 |
\Q | Begin literal sequence | \Q$10\E The price is $10 |
\E | End literal sequence | \Q$10\E The price is $10 |
Regex Component | Meaning | Example |
---|---|---|
\n | New line | Hello\nWorld Hello\nWorld |
\r | Carriage return | First line\rSecond line First line\rSecond line |
\t | Tab | Name:\tJohn Name: John |
\v | Vertical tab | First line\vSecond line First line\vSecond line |
\f | Form feed | ``Page 1\fPage 2` Page 1\fPage 2 |
\o | Octal character | \o123 123 |
\x | Hex character | \x41 41 |
Regex Component | Meaning | Example |
---|---|---|
. | Any character except new line (\n ) | c.t cAt, c1t |
(|) | Or | (cat|dog) cat, dog |
() | Group | (ab)+ abab, ababab |
(?:) | Passive (non-capturing) group | (?:abc)+ abc, abcabcabc |
[] | Range | [ae] a, e |
[^] | Not in range | [^ae] b, d |
[a-z] | Range of lowercase letters from a to z | [a-z]+ abc, hello |
[A-Z] | Range of uppercase letters from A to Z | [A-Z]+ ABC, HELLO |
[0-9] | Range of digits from 0 to 9 | [0-9]+ 123, 45 |
Regex Component | Meaning | Example |
---|---|---|
g | Global match | /cat/g cat, cats |
i | Case-insensitive (PCRE) | /cat/i cAt, Cat |
m | Multiple lines (PCRE) | /^cat/m cat\nconcatenat, cat\nsome text\ncat |
s | Treat string as single line (PCRE) | /cat.s/m cat\ns, cat\nsome text |
x | Allow comments and whitespace in pattern (PCRE) | /c a t/x concatenate |
e | Evaluate replacement (PCRE) | preg_replace('/(\d+)/e', '$1 * 2', '123 456') 246 912 |
U | Ungreedy pattern (PCRE) | /a.*b/U a test b |
Regex Component | Example |
---|---|
$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 |