Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Regex Tips & Tricks

Regex Tips & Tricks

Presented on December 6th, 2017 at the Confoo conference, Vancouver, Canada.
https://confoo.ca/en/yvr2017/
---------------------------------------------------------------
No matter whether you develop in PHP, Javascript, Ruby, Python or even HTML5, knowing how to write Regular Expressions is an essential skill for any developer.
While many consider regexes to be black magic, used appropriately they're an awesome power-tool in your tool-belt.
This talk will show you how to build effective regular expressions, introduce you to some of the more advanced features and teach you some useful tips and tricks.
---------------------------------------------------------------

Links:
https://www.pluralsight.com/courses/regular-expressions-fundamentals

Juliette Reinders Folmer

December 06, 2017
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. Regex Engines POSIX PCRE ECMAscript Oniguruma Boost DEELX RE2 TRE

    Pattwo GRETA GLib/ GRegex FREJ RGX QT CL-PPCRE Jakarta Henry Spencer’s regex
  2. Jamie Zawinski, August 1997 alt.religion.emacs Some people, when confronted with

    a problem, think "I know, I'll use regular expressions." Now they have two problems.
  3. 2. Nothing in life is to be feared. It is

    only to be understood. Marie Curie
  4. / / o on one one. one.* one.*s one.*s. one.*s.?

    one.*s.?t one.*s.?t [a-z] one.*s.?t[a-z]+ one.*s.?t[a-z]+p = space one.*s.?t[a-z]+p one.*s.?t[a-z]+p . one.*s.?t[a-z]+p . {2,} one.*s.?t[a-z]+p .{2,}, one.*s.?t[a-z]+p .{2,}, We take one step forward, two steps back ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
  5. Character classes PCRE POSIX [0-9] [^0-9] \d \D [[:digit:]] [^[:digit:]]

    [A-Za-z0-9_] [^A-Za-z0-9_] \w \W [[:word:]] [^[:word:]] [\t\f\r\n \v] [^\t\f\r\n \v] \s \S [[:space:]] [^[:space:]] [\t\f ] [^\t\f ] \h \H [[:blank:]] [^[:blank:]] [\r\n] [^\r\n] \v \V - -
  6. String delimiter - for prog language Regex delimiter - for

    regex - for prog language Meta-characters - for regex - for prog language What to Escape ?
  7. \[ \] \( \) \| \. \? \* \+ \{

    \} \^ \$ \\ \/ Literals [ ] ( ) | . ? * + { } ^ $ \ / (delimiter) Special Meaning Escaping Meta Characters
  8. [(] [)] [|] [.] [?][*][+][{][}] [$] [/] Literals [ ]

    ( ) | . ? * + { } ^ $ \ / (delimiter) Special Meaning Escaping Meta Characters
  9. Java String.quote() quoteReplacement() PHP preg_quote() Matlab regexptranslate() Python re.escape() Objective-C

    escapedTemplateForString() escapedPatternForString() Ruby Regexp.escape() Regexp.quote() Escaping Arbitrary Strings // Javascript: function escapeInputString( str ) { return str.replace(/[[\]\/\\{}()|?+^$*.-]/g, "\\$&"); }
  10. /^(( 25[0-5]| # Match 250-255 range 2[0-4][0-9]| # Match 200-249

    range [01]?[0-9]{1,2} # Match 0-199 range )\.){3} # Repeat 3 times with period (25[0-5]|2[0-4][0-9]|[01]?[0-9]{1,2}) # and once without $/x
  11. — Richard Feynman Know how to solve every problem that

    has been solved. What I cannot create, I do not understand. Photo by Gleick, J. Genius. p. 310f