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

ruby and regular expressions
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Ruby and Regular Expressions

Professor Larry Heimann Application Design & Development Information Systems Program

slide-2
SLIDE 2
slide-3
SLIDE 3

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.”

slide-4
SLIDE 4

Comic of the Day...

slide-5
SLIDE 5

Syntax of regex

slide-6
SLIDE 6

A simple regex

slide-7
SLIDE 7

Another way

slide-8
SLIDE 8

Substitution

slide-9
SLIDE 9

Substitution

slide-10
SLIDE 10

Time for a quiz ...

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Let’s look at the solution ...

slide-13
SLIDE 13

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

slide-14
SLIDE 14

Another challenge from a past exam ...

slide-15
SLIDE 15

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']

slide-16
SLIDE 16

Let’s look at the solution ...

slide-17
SLIDE 17

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']