Examples based Regex cheatsheet. Examples assumes /g
flag is on.
- Character Handling
- Anchors
- Escaped characters
- Quatifiers
- Groups
- Lookaround
- Flags
- Greedy vs Lazy Matching
Matches exact characters.
man => The man and woman.
Matches any character in the set.
[tT]he => The man and the woman. [bc]at => The cat and the bat and the rat are having a chat.
Matches any character that is not in the set.
[^bc]at => The cat and the bat and the rat are having a chat.
Matches any character between the the two specified character.
[0-9] => My ID number is 0123456789.
Matches any character except linebreaks.
.at => The cat and the bat and the rat are having a chat.
Equivalent: [^\n\r]
Matches any word (alphanumeric and underscore).
\w => PI (π) = 3.1416
Equivalent: [a-zA-Z0-9_]
Matches any non word (not alphanumeric and underscore).
\W => PI π = 3.1416
Equivalent: [^a-zA-Z0-9_]
Matches any digit.
\d => PI (π) = 3.1416
Equivalent: [0-9]
Matches any non digit.
\D => PI (π) = 3.1416
Equivalent: [^0-9]
Matches any whitespace.
\s => PI (π) = 3.1416
Matches any non whitespace.
\S => PI (π) = 3.1416
Matches at the beginning.
^P[iI] => PI (π) = 3.1416
Don't be confused with [^iT]
that means not any of i and I.
Matches at the end.
\d$ => PI (π) = 3.1416
Matches at the end of each word.
an\b => The man and woman.
Matches not at the end of each word.
an\B => The man and woman.
Matches special reserved characters +*?^$\.[]{}()|/
.
\(\S\) => PI (π) = 3.1416
Matches a TAB character.
\t => Tab1 Tab2
\n = Matches line feed character \v = Matches vertical tab character \f = Matches form feed character \r = Matches carriage return character \0 = Matches null character
© in octal is 251
\251 => RegExr is ©2014
© in hexadecimal is A9
\xA9 => RegExr is ©2014
© in unicode is 00A9
\u00A9 => RegExr is ©2014
Matches 0 or more of the preceding character.
\s*man\s* => The man and woman.
Matches 1 or more of the preceding character.
w\w+n => The man and woman.
Matches 0 or more of the preceding character.
[cC]olou?r => Color or colour?
Matches the specific quantity of the preceding character.
{3} = matches exactly 3
{1,3} = matches 1 to 3
{3,} = matches 3 or more
\d{3} => PI (π) = 3.1416 \d{1,4} => PI (π) = 3.1416 \d{2,} => PI (π) = 3.1416
Acts like an OR statment. Matches at expression level unlike []
[cbr]at|and => The cat and the bat and the rat are having a chat.
Groups characters and captures (stores) for futher processing.
(\d{4})-(\d{2})-(\d{2}) => Today is 2023-11-02.
Tags a group nam for the groups for futher processing.
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) => Today is 2023-11-02.
Matches the given group number only.
(\d{4})-(\d{2})-(\d{2})\1 => Today is 2023-11-02. # first group is referenced
Groups characters but doesn't capture.
(?:\d{4})-(?<month>\d{2})-(\d{2}) => Today is 2023-11-02. # captures two groups, one named "month" another unnamed
Get all the matches that are followed by a specific pattern.
\d(?=px) => 1pt 2px 3em 4px
Get all the matches that are not followed by a specific pattern.
\d(?!px) => 1pt 2px 3em 4px
Get all the matches that are preceded by a specific pattern.
(?<=name)\d => name1 age1 name2 sex1
get all the matches that are not preceded by a specific pattern.
(?<!name)\d => name1 age1 name2 sex1
Find all matches unlike stopping after the first match.
# default is just find one at => The cat and the bat and the rat are having a chat. # adding /g flag will search all /t/g => The cat and the bat and the rat are having a chat.
Case insensitive search.
/the/gi => The man and the woman.
Apply anchors(^ $ \b \B) at each line.
# default /at$/g => The cat and the bat and the rat # adding /m flag will match the anchors at each line /at$/gm => The cat and the bat and the rat
Dot (.) will match any character, including newline.
# default /.+/ => The man and the woman. # adding /s flag will match newline /.+/s => The man and the woman.
Default is greedy, that matches as many characters as possible.
.+at => The cat and the bat and the rat are having a chat.
Lazy makes is as few as possible.
.+?at => The cat and the bat and the rat are having a chat.