Home | Notes | Github |
---|
Written: 05-Mar-2022
Regex is useful. This cheat-sheet is a condensation of Amit Chaudhary’s useful article. Also of note/inspiration is this regex cheat-sheet
cat
matches any instance of the letters c
then a
then t
. This includes both cat the word and the first three letters of cathartic.
There are some special chars for fun things…
Groups:
[ab]
- Matches a
or b
(ab)
- Capture Group (like brackets in maths)
(png|jpg)
matches png
or jpg
Wildcards:
.
- Any char that’s not a new line (~= [a-zA-Z0-9]
)\d
- Digit (~= [0-9]
)\w
- Word char aka a letter (~=[a-zA-Z]
)\s
- (White)space\D
, \W
, \S
) invert the search.Iterations:
{n}
- n times of prev. char*
- Zero or more of prev. char (~={0,}
)+
- One or more of prev. char (~={1,}
)?
- Zero or one of prev. char (~={0,1}
)Anchors:
^
- Start of line$
- End of lineSpecial Chars
\
- Escape char
who?
would require who\?
If you want to match something thats defind by what or what does preced or procede then you need to use lookahead operators. There’s a good regex buddy article explaining this.
q(?=u)
- Positive look ahead (Queen
not Qatar
)q(?!u)
- Negative look ahead (Qatar
not Queen
)a(?<=b)
- Positive look behind: (lab
not cub
)a(?<!b)
- Negative Look behind: (cub
not lab
)Note look behind requires static length (no escapes or .*
).