Regex Tutorial PDF
Regex Tutorial PDF
Jonny Fox Follow
CTO@Factory Mind
Jun 23, 2017 · 6 min read
Regex tutorial — A quick cheatsheet
by examples
One of the most interesting features is that once you’ve learned the
syntax, you can actually use this tool in (almost) all programming
languages (JavaScript, Java, VB, C #, C / C++, Python, Perl, Ruby,
Delphi, R, Tcl, and many others) with the slightest distinctions about
the support of the most advanced features and syntax versions
supported by the engines).
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 1/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
Basic topics
Anchors — ^ and $
Quanti ers — * + ? and {}
OR operator — | or []
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 2/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
Character classes — \d \w \s and .
For example, \D will perform the inverse match with respect to that
obtained with \d .
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 3/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
Notice that you can match also non-printable characters like tabs
\t , new-lines \n , carriage returns \r .
Flags
We are learning how to construct a regex but forgetting a fundamental
concept: ags.
A regex usually comes with in this form /abc/, where the search
pattern is delimited by two slash characters / . At the end we can
specify a ag with these values (we can also combine them each other):
• g (global) does not return after the rst match, restarting the
subsequent searches from the end of the previous match
• m (multi line) when enabled ^ and $ will match the start and
end of a line, instead of the whole string
. . .
Intermediate topics
Grouping and capturing — ()
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 4/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
Bracket expressions — []
Greedy and Lazy match
The quanti ers ( * + {} ) are greedy operators, so they expand the
match as far as they can through the provided text.
Notice that a better solution should avoid the usage of . in favor of a
more strict regex:
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 5/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
. . .
Advanced topics
Boundaries — \b and \B
It comes with the its negation, \B . This matches all positions where
\b doesn’t match and could be if we want to nd a search pattern
fully surrounded by word characters.
Back-references — \1
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 6/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
Look-ahead and Look-behind — (?=) and (?<=)
Summary
As you’ve seen, the application elds of regex can be multiple and I’m
sure that you’ve recognized at least one of these tasks among those
seen in your developer career, here a quick list:
• string parsing (for example catch all url’s GET parameters, capture
text inside a set of parenthesis)
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 7/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
Have fun and do not forget to recommend the article if you liked it
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 8/9
2018/9/20 Regex tutorial — A quick cheatsheet by examples – Factory Mind – Medium
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 9/9