Pro Yearly is on sale from $80 to $50! »

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

2776198ea9584b6c0d4b494293b8d635?s=128

Juliette Reinders Folmer

September 16, 2016
Tweet

Transcript

  1. Regular Expression Fundamentals Juliette Reinders Folmer @jrf_nl regexcheatsheets.com

  2. pluralsight.com

  3. Wildcards on Steriods

  4. Pattern Recognition

  5. 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
  6. Whitelisting Blacklisting Input string Input string ? ?

  7. Typical Uses for Regular Expressions Search (and replace) String parsing

    Data mapping Syntax highlighting Data scraping Input validation
  8. (Sys-)Admins File system Server directives Data Professionals Query data Developers

    Working with strings Users of Regular Expressions
  9. How It Works

  10. Subject String Subject

  11. How It Works Pattern Regex Subject Function Engine Result

  12. What are the matches ? How many matches have been

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

    Pattwo GRETA GLib/ GRegex FREJ RGX QT CL-PPCRE Jakarta Henry Spencer’s regex
  14. Syntax Overlap

  15. Still with me ?

  16. Terminology /[a-z0-9]+/im Regular Expression /[a-z0-9]+/im Delimiters /[a-z0-9]+/im Modifiers

  17. 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
  18. None
  19. The Pattern # AB 12 34

  20. None
  21. Visualization of the Pattern

  22. Tips & Tricks

  23. Photo by Scott Liddell 1. If you need a screwdriver,

    why use a hammer ?
  24. 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.
  25. 2. Not all matches are made in heaven... Photo by

    Petr Kratochvil
  26. 3. Only Elephants Remember Everything © Photo by Juliette Reinders

    Folmer
  27. (?:<expr>)

  28. 4. Being negative isn't always a bad thing © Photo

    by Juliette Reinders Folmer
  29. [^<chars>]

  30. Less is the new more 5

  31. / / 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 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
  32. / / We take one step back, two steps forward

  33. <unit>{,m}? <unit>{n,}? <unit>{n,m}? <unit>*? <unit>+? <unit>?? Reluctant Quantifiers

  34. 6. Explore Your Boundaries Photo by Miguel A.C. Domingo

  35. Beginning of string Beginning of line Word boundaries End of

    string End of line
  36. 7. The first love is the deepest...

  37. /#?([A-F0-9]{6}|[A-F0-9]{3})/i

  38. 8. What's this global village people keep talking about ???

  39. 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 - -
  40. déjà vu [\w ]+ French (fr) déjà vu [\w ]+

    English (en)
  41. 9. Escape and escape again

  42. \[ \] \( \) \| \. \? \* \+ \{

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

    ( ) | . ? * + { } ^ $ \ / (delimiter) Special Meaning Escaping Meta Characters
  44. 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, "\\$&"); }
  45. Matching a Literal Backslash \\\\ The actual backslash \\\\ Escaping

    for use in regex \\\\ String escape
  46. Modify your behaviour 10

  47. None
  48. Setting: Unsetting: Combined: Apply to subpattern (non-capturing): Inline Modifiers (?i)

    (?-i) (?im-sx) (?i:subp)
  49. None
  50. Advanced Features Look around Named sub-matches Conditional sub-patterns Recursion Inline

    comments
  51. Thanks! Any questions ? Slides: https://speakerdeck.com/jrf Course: https://www.pluralsight.com/courses/ regular-expressions-fundamentals