Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

@maccath | Katy Ereira | #WPLdn - August 2017 What is Regex?

Slide 3

Slide 3 text

@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

Slide 4

Slide 4 text

@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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

@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

Slide 9

Slide 9 text

@maccath | Katy Ereira | #WPLdn - August 2017 Setting the Foundations

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

@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

Slide 39

Slide 39 text

@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

Slide 40

Slide 40 text

@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

Slide 41

Slide 41 text

@maccath | Katy Ereira | #WPLdn - August 2017 Let the games begin!

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

@maccath | Katy Ereira | #WPLdn - August 2017 Thank you!

Slide 48

Slide 48 text

@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