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

Regex Fundamentals

Regex Fundamentals

Presented on September 16 2016 at Codemotion Warsaw, Warsaw, Poland.
http://warsaw2016.codemotionworld.com/
---------------------------------------------------------------
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, 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 of the trade.
---------------------------------------------------------------

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

Juliette Reinders Folmer

September 16, 2016
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. Serial numbers Barcodes Flight numbers CSV files Log files Email

    headers Twitter handles Facebook username Skype usernames MD5 hash Sentences Good passwords Isbn numbers HTML code Html tags Html attributes CSS code Urls Email addresses File names File extensions Directory paths Postal codes Telephone numbers Number plates Credit card numbers Bank account numbers Mathematical formulas Elements from the periodical table Patterns in text strings
  2. Typical Uses for Regular Expressions Search (and replace) String parsing

    Data mapping Syntax highlighting Data scraping Input validation
  3. What are the matches ? How many matches have been

    found ? Does it match ? Result Types
  4. Regex Engines POSIX PCRE ECMAscript Oniguruma Boost DEELX RE2 TRE

    Pattwo GRETA GLib/ GRegex FREJ RGX QT CL-PPCRE Jakarta Henry Spencer’s regex
  5. A a 1 . ? * + {#} [...] (

    ... | ... ) ^ ... $ \w \d \s g m s i  Literals  Wildcard  Quantifiers  Character ranges  Grouping and alternation  Anchors  Shorthand character codes  Modifiers Basic Syntax
  6. 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.
  7. / / 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 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
  8. 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 - -
  9. \[ \] \( \) \| \. \? \* \+ \{

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

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