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

regex in ruby

regex in ruby

a little look at regex features in ruby

Avatar for Melissa Kaulfuss

Melissa Kaulfuss

October 18, 2017

More Decks by Melissa Kaulfuss

Other Decks in Technology

Transcript

  1. array = [“cat”, “string_b”, “string_124”]
 
 => [“cat”, “string_b”, “string_124”]

    patterns = [/map/, /_b/, /\d+/] 
 => [/a-z+/, /_b/, /\d+/] regex = Regexp.union(patterns)
  2. array = [“cat”, “string_b”, “string_124”]
 
 => [“cat”, “string_b”, “string_124”]

    patterns = [/map/, /_b/, /\d+/] 
 => [/a-z+/, /_b/, /\d+/] regex = Regexp.union(patterns) 
 => /(?-mix:a-z+)|(?-mix:_b)|(?-mix:\d+)/
  3. array = [“cat”, “string_b”, “string_124”]
 
 => [“cat”, “string_b”, “string_124”]

    patterns = [/map/, /_b/, /\d+/] 
 => [/a-z+/, /_b/, /\d+/] regex = Regexp.union(patterns) 
 => /(?-mix:a-z+)|(?-mix:_b)|(?-mix:\d+)/ m = multiline mode i = case insensitive x = extended mode
  4. array = [“cat”, “string_b”, “string_124”]
 
 => [“cat”, “string_b”, “string_124”]

    patterns = [/map/, /_b/, /\d+/] 
 => [/a-z+/, /_b/, /\d+/] regex = Regexp.union(patterns) 
 => /(?-mix:a-z+)|(?-mix:_b)|(?-mix:\d+)/ array.delete_if {|string| string.match(regex)}

  5. array = [“cat”, “string_b”, “string_124”]
 
 => [“cat”, “string_b”, “string_124”]

    patterns = [/map/, /_b/, /\d+/] 
 => [/a-z+/, /_b/, /\d+/] array.delete_if {|string| string.match(regex)}
 
 => [“cat”] regex = Regexp.union(patterns) 
 => /(?-mix:a-z+)|(?-mix:_b)|(?-mix:\d+)/
  6. Named captures! 
 Writing the Regexp so it can span

    multiple lines! Changing the delimiter so you can use / (e.g. `%r{http://}`)
 
 I also like named capture groups & any feature that makes it 
 more obvious to someone reading the regex/code & what you’re 
 trying to match https://krillapps.com/patterns/ 
 Best few dollars I ever spent! Makes composing regex’s much easier!
 When dealing with regex, https://regex101.com is quintessential “ ” “ ” Hello, what’s neat about regex?
 Experienced Ruby Developer Person?
  7. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/
  8. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ => /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/
  9. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ => /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ match = regex.match(string_to_match)
  10. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ => /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ match = regex.match(string_to_match) => <MatchData "Mac Book Pro - Quantity: 1 Price: 2000.00" quantity:"1" price:"2000.00">
  11. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ => /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ match = regex.match(string_to_match) => <MatchData "Mac Book Pro - Quantity: 1 Price: 2000.00" quantity:"1" price:"2000.00"> match[:quantity]
  12. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ => /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ match = regex.match(string_to_match) => <MatchData "Mac Book Pro - Quantity: 1 Price: 2000.00" quantity:"1" price:"2000.00"> match[:quantity] => "1"
  13. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ => /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ match = regex.match(string_to_match) => <MatchData "Mac Book Pro - Quantity: 1 Price: 2000.00" quantity:"1" price:"2000.00"> match[:quantity] => "1" match[:price]
  14. string_to_match = 'Mac Book Pro - Quantity: 1 Price: 2000.00'

    regex = /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ => /[\w\s]+ - Quantity: (?<quantity>\d+) Price: (?<price>[\d\.]+)/ match = regex.match(string_to_match) => <MatchData "Mac Book Pro - Quantity: 1 Price: 2000.00" quantity:"1" price:"2000.00"> match[:quantity] => "1" match[:price] => "2000.00"
  15. https://krillapps.com/patterns/ ‘’Best few dollars I ever spent! 
 Makes composing

    regex’s much easier.’’ - Joshua Paling https://regex101.com/ ‘’When dealing with Regex, regex101
 is quintessential!’’ - Colby Swandale
  16. Regular expressions are like a particularly spicy hot sauce -

    
 to be used in moderation and with restraint only when appropriate. Jeff Atwood @codinghorror