CS 330 Lecture 4 – Regular Expressions
Agenda
- what ?s
- the gauntlet
- state machines/DFAs
- DFAs are regular expressions
- bingo
TODO
- Walk through http://regexone.com.
- Write expressions to match:
- Lines ending with a hyphenated word.
- Words with an internal uppercase letter.
- Lines lacking a semi-colon at their close. Don’t match lines that have a semi-colon followed by whitespace.
- Instances of the identifier
i
. Don’t match other occurrences ofi
.
- 1/4 sheet.
While You’re Waiting
- We’re going to play some regex bingo later on. You and a partner make a 3×3 grid of randomly generated strings. Include upper- and lowercase letters, numbers, whitespace, and punctuation. Keep the strings short. There’s no free space.
State Machines
Code
show_matches.rb
#!/usr/bin/env ruby
txt = IO.read('eg.txt')
puts txt.scan(/[0-9]/)
puts '--------------'
puts txt.scan(/\S+@\S+/)
puts '--------------'
puts txt.scan(/\.\s+[a-z]/)
puts '--------------'
puts txt.scan(/<b>.*<\/b>/)
puts '--------------'
puts txt.scan(/<.*?>/)
eg.txt
asdf asdf 1 234 456
asdfkl sdf kdfklads f <b>56</b>
johnch@uwec.edu
asdfk foo@foo.com
The dog is <b>luscious</b>. The cat is blah. those who walk on rainbows peak at middle age.
Haiku
Can brains handle X?
Brains have handled X for years
They’re on to Y now
Brains have handled X for years
They’re on to Y now