Regex Cheet sheet
Today we are going to see a Weird Thing. Yeah we are gonna learn Regex Syntax.
NOTE: The highlighted bold characters are the results.
- Character Classes
. => any character except newline
pattern = .
result = glib jocks vex dwarves!
\w \d \s => word, digit, whitespace
\W \D \S => not word, digit, whitespace
pattern = \w
result = bonjour, mon frère
[abc] => any of a, b, or c
pattern = [aeiou]
result = glib jocks vex dwarves!
[^abc] => not a, b, or c
pattern = [^aeiou]
result = glib jocks vex dwarves!
[a-g] => character between a & g
pattern = [g-s]
result = abcdefghijklmnopqrstuvwxyz
2. Anchors
^abc$ => start / end of the string
pattern = ^\w+
result = she sells seashellspattern = \w+$
result = she sells seashells
\b \B => word, not-word boundary
pattern = s\b
result = she sells seashellspattern = s\B
result = she sells seashells
3. Escaped Characters
\. \* \\ => escaped special characters\t \n \r => tab, linefeed, carriage return
4. Groups and Look Around
(abc) => capture group
pattern = (ha)+
result = hahaha haa hah!
\1 => back reference to group #1
pattern = (\w)a\1
result = hah dad bad dab gag gab
(?:abc) => non-capturing group(?=abc) => positive lookahead(?!abc) => negative lookahead
5.Quantifiers and Alternations
a* a+ a? => 0 or more, 1 or more, 0 or 1
pattern = b\w+
result = b be bee beer beerspattern = b\w*
result = b be bee beer beerspattern = colou?r
result = color colour
a{5} a{2,} => exactly five, two or morea{1,3} => between one & three
pattern = b\w{2,3}
result = b be bee beer beers
a+? a{2,}? => match as few as possible
pattern = b\w+?
result = b be bee beer beers
ab|cd => match ab or cd
pattern = b(a|e|i)d
result = bad bud bod bed bid
That’s it for now. Keep Learning.