Fun and Games with Regular Expressions

Fun and Games with Regular Expressions

Regex, fun? Not likely!

F868a7d36323e80a7cf37e79d8e5c370?s=128

Katy Ereira

August 31, 2017
Tweet

Transcript

  1. @maccath | Katy Ereira | #WPLdn - August 2017 Fun

    & Games with Regex Regex, fun? Not likely!
  2. @maccath | Katy Ereira | #WPLdn - August 2017 What

    is Regex?
  3. @maccath | Katy Ereira | #WPLdn - August 2017 Regular

    Expressions are... • A string of symbols describing a search pattern • Known as ‘regex’ or ‘regexp’ for short • Super powerful • Complicated looking • Not Fun
  4. @maccath | Katy Ereira | #WPLdn - August 2017 Regex

    are used for... • Searching within text • Finding and replacing within text • Data validation • Data extraction and general wrangling • Not Games
  5. @maccath | Katy Ereira | #WPLdn - August 2017 A

    basic example Defining a valid user name /^[a-z0-9_-]{3,16}$/
  6. @maccath | Katy Ereira | #WPLdn - August 2017 As

    a diagram Regex visualisation by https://regexper.com
  7. @maccath | Katy Ereira | #WPLdn - August 2017 Usage

    instructions /^[a-z0-9_-]{3,16}$/ my_username spec*alChars~ a-really-damn-long-username
  8. @maccath | Katy Ereira | #WPLdn - August 2017 A

    less basic example Defining a valid email address \A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)* |"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0 b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](? :[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){ 3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]: (?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\ x0e-\x7f])+)\])\z Regex of RFC5322 by http://www.regular-expressions.info/email.html
  9. @maccath | Katy Ereira | #WPLdn - August 2017 Setting

    the Foundations
  10. @maccath | Katy Ereira | #WPLdn - August 2017 Simple

    string matching /bar/ barnacle capybara kangaroo
  11. @maccath | Katy Ereira | #WPLdn - August 2017 Simple

    string matching /bar/ barnacle capybara kangaroo
  12. @maccath | Katy Ereira | #WPLdn - August 2017 Anchors

    and word boundaries /^a/ afghan hound akita chihuahua
  13. @maccath | Katy Ereira | #WPLdn - August 2017 Anchors

    and word boundaries /^a/ afghan hound akita chihuahua
  14. @maccath | Katy Ereira | #WPLdn - August 2017 Anchors

    and word boundaries /a$/ afghan hound akita chihuahua
  15. @maccath | Katy Ereira | #WPLdn - August 2017 Anchors

    and word boundaries /a$/ afghan hound akita chihuahua
  16. @maccath | Katy Ereira | #WPLdn - August 2017 Sets

    /[end]$/ siamese sphynx persian
  17. @maccath | Katy Ereira | #WPLdn - August 2017 Sets

    /[end]$/ siamese sphynx persian
  18. @maccath | Katy Ereira | #WPLdn - August 2017 Ranges

    /[a-g]/ siamese sphynx burmese
  19. @maccath | Katy Ereira | #WPLdn - August 2017 Ranges

    /[a-g]/ siamese sphynx burmese
  20. @maccath | Katy Ereira | #WPLdn - August 2017 Repetition

    /^[a-g]+/ siamese sphynx absynnian
  21. @maccath | Katy Ereira | #WPLdn - August 2017 Repetition

    /^[a-g]+/ siamese sphynx absynnian
  22. @maccath | Katy Ereira | #WPLdn - August 2017 Repetition

    /^[a-c]*/
  23. @maccath | Katy Ereira | #WPLdn - August 2017 Repetition

    /^[a-g]*s/ sphynx persian absynnian
  24. @maccath | Katy Ereira | #WPLdn - August 2017 Repetition

    /^[a-g]*s/ sphynx persian absynnian
  25. @maccath | Katy Ereira | #WPLdn - August 2017 Negative

    Sets /^[^a-g]/ ice cream biscuit cake
  26. @maccath | Katy Ereira | #WPLdn - August 2017 Negative

    Sets /^[^a-g]/ ice cream biscuit cake
  27. @maccath | Katy Ereira | #WPLdn - August 2017 Alternation

    /^(ca|ke)/ caramel toffee kendal mint cake
  28. @maccath | Katy Ereira | #WPLdn - August 2017 Alternation

    /^(ca|ke)/ caramel toffee kendal mint cake
  29. @maccath | Katy Ereira | #WPLdn - August 2017 Character

    classes /.+’s/ Cadbury Creme Egg Fry’s Turkish Delight Terry’s chocolate orange
  30. @maccath | Katy Ereira | #WPLdn - August 2017 Character

    classes /.+’s/ Cadbury Creme Egg Fry’s Turkish Delight Terry’s chocolate orange
  31. @maccath | Katy Ereira | #WPLdn - August 2017 Character

    classes /\w+’s/ Cadbury Creme Egg Fry’s Turkish Delight Terry’s chocolate orange
  32. @maccath | Katy Ereira | #WPLdn - August 2017 Character

    classes /\D+’s/ Cadbury Creme Egg Fry’s Turkish Delight Terry’s chocolate orange
  33. @maccath | Katy Ereira | #WPLdn - August 2017 Character

    classes /\D+’s\s/ Cadbury Creme Egg Fry’s Turkish Delight Terry’s chocolate orange
  34. @maccath | Katy Ereira | #WPLdn - August 2017 Escaping

    /[\(\[\]]\w+[\)\[\]]/ (Escape) From Alcatraz The Great [Escape] Escape from New York
  35. @maccath | Katy Ereira | #WPLdn - August 2017 Escaping

    /[\(\[\]]\w+[\)\[\]]/ (Escape) From Alcatraz The Great [Escape] Escape from New York
  36. @maccath | Katy Ereira | #WPLdn - August 2017 Capturing

    Groups and Backreferences /(.+)\s\1/ Wet Wet Wet Bow Wow Wow The B-52s
  37. @maccath | Katy Ereira | #WPLdn - August 2017 Capturing

    Groups and Backreferences /(.+)\s\1/ Wet Wet Wet Bow Wow Wow The B-52s
  38. @maccath | Katy Ereira | #WPLdn - August 2017 Search

    and replace /\w+’s/ Fry’s Turkish Delight My Turkish Delight Terry’s chocolate orange My chocolate orange My
  39. @maccath | Katy Ereira | #WPLdn - August 2017 Capturing

    Groups and References /\w+’s/ Fry’s Turkish Delight Not Fry’s Turkish Delight Terry’s chocolate orange Not Terry’s chocolate orange Not $0
  40. @maccath | Katy Ereira | #WPLdn - August 2017 Capturing

    Groups and References /(\w+’s)/ Fry’s Turkish Delight Not Fry’s Turkish Delight Terry’s chocolate orange Not Terry’s chocolate orange Not $1
  41. @maccath | Katy Ereira | #WPLdn - August 2017 Let

    the games begin!
  42. @maccath | Katy Ereira | #WPLdn - August 2017 Game

    #1: Regex Golf play along at: http://alf.nu/regexGolf
  43. @maccath | Katy Ereira | #WPLdn - August 2017 xkcd

    - https://xkcd.com/1313/
  44. @maccath | Katy Ereira | #WPLdn - August 2017 Game

    #2: Regex Crosswords play along at: https://regexcrossword.com
  45. @maccath | Katy Ereira | #WPLdn - August 2017 Game

    #3: Regex Challenges play along at: https://callumacrae.github.io/regex-tuesday/challenge12.html
  46. @maccath | Katy Ereira | #WPLdn - August 2017 xkcd

    - https://xkcd.com/208/
  47. @maccath | Katy Ereira | #WPLdn - August 2017 Thank

    you!
  48. @maccath | Katy Ereira | #WPLdn - August 2017 Fun

    Regex Resources • http://www.regular-expressions.info - All about Regex • http://regexr.com - Learn, build and test Regular Expressions • https://regexone.com - Interactive Regex tutorial • https://www.hackerrank.com/domains/regex/ - Regex challenges • https://callumacrae.github.io/regex-tuesday/ - More regex challenges • https://regexcrossword.com - Regex Crossword • http://alf.nu/RegexGolf - Regex Golf