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

Fun and Games with Regular Expressions

Fun and Games with Regular Expressions

Regex, fun? Not likely!

Katy Ereira

August 31, 2017
Tweet

More Decks by Katy Ereira

Other Decks in Technology

Transcript

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

    & Games with Regex Regex, fun? Not likely!
  2. @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
  3. @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
  4. @maccath | Katy Ereira | #WPLdn - August 2017 A

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

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

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

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

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

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

    and word boundaries /^a/ afghan hound akita chihuahua
  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 Sets

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Groups and Backreferences /(.+)\s\1/ Wet Wet Wet Bow Wow Wow The B-52s
  31. @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
  32. @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
  33. @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
  34. @maccath | Katy Ereira | #WPLdn - August 2017 Game

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

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

    #3: Regex Challenges play along at: https://callumacrae.github.io/regex-tuesday/challenge12.html
  37. @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