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

Regex Tips & Tricks

Regex Tips & Tricks

Presented on February 9th, 2017 at the OrlandoDevs meetup, Orlando, Florida, US.
https://www.meetup.com/OrlandoDevs/
---------------------------------------------------------------
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 a lot of people consider regexes to be black magic, used appropriately they're an awesome power-tool in your tool-belt. This talk will take you on a little walk on the wild side, introduce you to some of the more advanced features and teach you some useful tips and tricks of the trade.
---------------------------------------------------------------

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

Juliette Reinders Folmer

February 09, 2017
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. 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.
  2. / / 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 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
  3. 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 - -
  4. [(] [)] [|] [.] [?][*][+][{][}] [$] [/] \[ \] \(

    \) \| \. \? \* \+ \{ \} \^ \$ \\ \/ Literals [ ] ( ) | . ? * + { } ^ $ \ / (delimiter) Special Meaning Escaping Meta Characters
  5. 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, "\\$&"); }