Slide 1

Slide 1 text

Regular Expressions with Ruby Elle Meredith – @aemeredith

Slide 2

Slide 2 text

Test Extract Change

Slide 3

Slide 3 text

/fox/ =~ "The quick brown fox" Basic Matching

Slide 4

Slide 4 text

/fox/ =~ "The quick brown fox" => 16 Basic Matching

Slide 5

Slide 5 text

/cat/ =~ "The quick brown fox" => nil Basic Matching

Slide 6

Slide 6 text

/cat/ !~ "The quick brown fox" => true Basic Matching

Slide 7

Slide 7 text

MatchData string = "The quick brown fox jumps over the lazy dog”

Slide 8

Slide 8 text

MatchData string = "The quick brown fox jumps over the lazy dog” ! matchdata = string.match /fox/ => #

Slide 9

Slide 9 text

MatchData matchdata.to_s => "fox"

Slide 10

Slide 10 text

MatchData matchdata.pre_match => "The quick brown "

Slide 11

Slide 11 text

MatchData matchdata.post_match => " jumps over the lazy dog"

Slide 12

Slide 12 text

Captures string = "03APR2014" ! string.match /\D{3}/

Slide 13

Slide 13 text

Captures string = "03APR2014" ! string.match /\D{3}/

Slide 14

Slide 14 text

Captures string = "03APR2014" ! string.match /\D{3}/

Slide 15

Slide 15 text

Captures string = "03APR2014" ! string.match /\D{3}/ => #

Slide 16

Slide 16 text

Captures md = string.match /(.*)(\D{3})(.*)/

Slide 17

Slide 17 text

Captures md = string.match /(.*)(\D{3})(.*)/

Slide 18

Slide 18 text

Captures md = string.match /(.*)(\D{3})(.*)/

Slide 19

Slide 19 text

Captures md = string.match /(.*)(\D{3})(.*)/ => #

Slide 20

Slide 20 text

Captures md.captures => ["03", "APR", "2014"]

Slide 21

Slide 21 text

Captures md = string.match /(.*)(\D{3})(.*)/ => #

Slide 22

Slide 22 text

Captures md[1] => "03" md[2] => "APR" md[3] => "2014"

Slide 23

Slide 23 text

Named Captures md = string.match /(?.*)(? \D{3})(?.*)/ => #

Slide 24

Slide 24 text

Named Captures md = string.match /(?.*)(? \D{3})(?.*)/ => #

Slide 25

Slide 25 text

Named Captures md = string.match /(?.*)(? \D{3})(?.*)/ => #

Slide 26

Slide 26 text

Named Captures md = string.match /(?.*)(? \D{3})(?.*)/ => #

Slide 27

Slide 27 text

Named Captures md['day'] => "03"

Slide 28

Slide 28 text

Look Around string = ''' I love my job, I love the pay! I love it more and more each day. I love my boss, he is the best! I love his boss and all the rest. '''

Slide 29

Slide 29 text

Look Around string.scan /love my/ => ["love my", "love my”]

Slide 30

Slide 30 text

Positive Lookahead string.scan /love my (?=job)/ => ["love my "]

Slide 31

Slide 31 text

Changing things string.gsub(/love/, 'hate')

Slide 32

Slide 32 text

Changing things string.gsub(/love/, 'hate') => "\nI hate my job, I hate the pay! \nI hate it more and more each day.\nI hate my boss, he is the best!\nI hate his boss and all the rest.\n"

Slide 33

Slide 33 text

Changing things string.gsub!(/\she/, ' she').gsub!(/ his/, 'her')

Slide 34

Slide 34 text

Changing things string.gsub!(/\she/, ' she').gsub!(/ his/, 'her')

Slide 35

Slide 35 text

Changing things string.gsub!(/\she/, ' she').gsub!(/ his/, ‘her') => "\nI love my job, I love the pay! \nI love it more and more each day. \nI love my boss, she is the best!\nI love her boss and all the rest.\n"

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Regular Expressions with Ruby Elle Meredith – @aemeredith