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 strings, according to certain syntax rules.”
Comic of the Day...
Syntax of regex
A simple regex
Another way
Substitution
Substitution
Time for a quiz ...
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
Let’s look at the solution ...
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
Another challenge from a past exam ...
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']
Let’s look at the solution ...
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