ruby and regular expressions
play

Ruby and Regular Expressions Professor Larry Heimann Application - PowerPoint PPT Presentation

Ruby and Regular Expressions Professor Larry Heimann Application Design & Development Information Systems Program What are regular expressions? In computing, a regular expression is a string that is used to describe or match a set of


  1. Ruby and Regular Expressions Professor Larry Heimann Application Design & Development Information Systems Program

  2. What are regular expressions? “In computing, a regular expression is a string that is used to describe or match a set of strings, according to certain syntax rules.”

  3. Comic of the Day...

  4. Syntax of regex

  5. A simple regex

  6. Another way

  7. Substitution

  8. Substitution

  9. Time for a quiz ...

  10. hexclr_pattern = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ "#287492".match(hexclr_pattern) # => T or F "#bc8201".match(hexclr_pattern) # => T or F "403291".match(hexclr_pattern) # => T or F "#BB9".match(hexclr_pattern) # => T or F "#ACGDAB".match(hexclr_pattern) # => T or F "#ECAA899".match(hexclr_pattern) # => T or F "#FfF000".match(hexclr_pattern) # => T or F "#ABCDEF".match(hexclr_pattern) # => T or F "#A9-Fa-f0".match(hexclr_pattern) # => T or F "#Aa0eD8".match(hexclr_pattern) # => T or F "Aa-0e-D8".match(hexclr_pattern) # => T or F "82A".match(hexclr_pattern) # => T or F "Hex: #FFF".match(hexclr_pattern) # => T or F "Hex: #JFK ".match(hexclr_pattern) # => T or F

  11. Let’s look at the solution ...

  12. hexclr_pattern = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ "#287492".match(hexclr_pattern) # => T or F "#bc8201".match(hexclr_pattern) # => T or F "403291".match(hexclr_pattern) # => T or F "#BB9".match(hexclr_pattern) # => T or F "#ACGDAB".match(hexclr_pattern) # => T or F "#ECAA899".match(hexclr_pattern) # => T or F "#FfF000".match(hexclr_pattern) # => T or F "#ABCDEF".match(hexclr_pattern) # => T or F "#A9-Fa-f0".match(hexclr_pattern) # => T or F "#Aa0eD8".match(hexclr_pattern) # => T or F "Aa-0e-D8".match(hexclr_pattern) # => T or F "82A".match(hexclr_pattern) # => T or F "Hex: #FFF".match(hexclr_pattern) # => T or F "Hex: #JFK ".match(hexclr_pattern) # => T or F

  13. Another challenge from a past exam ...

  14. In lab we built a regex tester that took in an array of cases and tested each against a pattern provided. Assuming we are using that program, provide a regex pattern that will successfully match against the .pass array but also not match those in the .fail array. Add the pattern that will pass all .pass cases and not pass all .fail cases listed below: phone = RegexTester.new( ) __________________________________________________________________ phone.pass = ['4123456789', '412-456-7890', '412.456.7890', '(412) 345-6789', '412 345-6789'] phone.fail = ['14123456789', '412-EAT-FOOD', '412.4567.890', '345-6789']

  15. Let’s look at the solution ...

  16. In lab we built a regex tester that took in an array of cases and tested each against a pattern provided. Assuming we are using that program, provide a regex pattern that will successfully match against the .pass array but also not match those in the .fail array. Add the pattern that will pass all .pass cases and not pass all .fail cases listed below: phone = RegexTester.new(/^\(?\d{3}\)?[ .-]?\d{3}[.-]?\d{4}$/) __________________________________________________________________ phone.pass = ['4123456789', '412-456-7890', '412.456.7890', '(412) 345-6789', '412 345-6789'] phone.fail = ['14123456789', '412-EAT-FOOD', '412.4567.890', '345-6789']

Recommend


More recommend