Regex-Fu

 Regex-Fu

Presented on May 8th, 2018 at Codemotion Amsterdam, The Netherlands.
https://amsterdam2018.codemotionworld.com/
---------------------------------------------------------------
Regular expression, you either hate them or you love them, but do you really know how to harness their power ? Based on the PCRE implementation, this talk will show you how to get the most out of your /^regex(es)?$/, how switches affect your results, how to be less greedy, how to assert your power and let's not forget: when *not* to use regex.
---------------------------------------------------------------

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

Transcript

  1. RegEx-Fu Juliette Reinders Folmer @jrf_nl AMSTERDAM | MAY 8-9, 2018

  2. None
  3. Wildcards on Steroids

  4. Pattern Recognition

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

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

  7. PCRE

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

  9. Tips & Tricks

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

    why use a hammer ?
  11. 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.
  12. 2. Nothing in life is to be feared. It is

    only to be understood. Marie Curie
  13. Whitelisting Blacklisting Input string Input string ? ?

  14. 3. Not all matches are made in heaven... Photo by

    Petr Kratochvil
  15. 4. Only Elephants Remember Everything © Photo by Juliette Reinders

    Folmer
  16. (?:<expr>)

  17. Less is the new more 5

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

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

  21. 6. Being negative isn't always a bad thing © Photo

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

  23. 7. Explore Your Boundaries Photo by Miguel A.C. Domingo

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

    string End of line
  25. 8. The first love is the deepest...

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

  27. 9. What's this global village people keep talking about ???

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

    English (en)
  30. 10. Escape and escape again

  31. String delimiter - for prog language Regex delimiter - for

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

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

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

    for use in regex \\\\ String escape
  36. Modify your behaviour 11

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

    (?-i) (?im-sx) (?i:subp)
  39. Explore

  40. None
  41. /^(( 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
  42. None
  43. [0] – Complete match [1] – Match against sub-pattern 1

    [2] – Match against sub-pattern 2 [3] – Match against sub-pattern 3 ... Match Array Photo by Petr Kratochvil
  44. (?<name><expr>) (?P>name)

  45. [0] – Complete match [firstname] – Match against named sub-pattern

    firstname [lastname] – Match against named sub-pattern lastname ... Match Array Photo by Petr Kratochvil
  46. Image by Gerd Altmann

  47. — 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
  48. Advanced Features Look around Conditional sub-patterns Recursion Inline comments

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