CS 330 Lecture 8 – Regex Closeout
Agenda
- what ?s
- regex bingo
- a few more examples
- links between images
- double-spaces between sentences
- finding repeats
- ith field
TODO
Right now:
- You and a partner make a 4×4 grid of randomly generated strings. Include upper- and lowercase letters, numbers, whitespace, and punctuation. Keep the strings short. There’s no free space.
Before Friday:
- Familiarize yourself with grammars and ANTLR. Read Grammar: The Language of Languages and Getting Started with ANTLR. On a 1/4 sheet, write a little grammar to match a limited set of variable “declassignments”—variable declarations that also initialize their values. Allow for
int
s,string
s, andboolean
s. Support only literals on the right-hand side (int a = 5
,boolean isYesterday = true
,string name = "bingo"
). Don’t worry about type checking. That belongs in the semantic analysis stage.
Code
links.rb
#!/usr/bin/env ruby
text = IO.read(ARGV[0])
text.gsub!(/<img.*?src="(.*?)".*?>/) do |tag|
"<a href=\"#{$1}\">#{tag}</a>"
end
puts text
repeats.rb
#!/usr/bin/env ruby
text = "the the the quick quick brown brown brown fox fox jumps jumps jumps over over the the the the the the the the lazy lazy lazy dog dog dog dog"
# puts text.gsub(/(\w+)( \1)+/, '\1')
puts text.gsub(/(\w+) (?=\1)/, '')
fixfix.rb
#!/usr/bin/env ruby
text = "A bee flew. Dr. Neeper sneezes. Ben went to St. John's. I didn't."
puts text.gsub(/(?<!Dr|St)\. /, '. ')
Haiku
on triumph
This tweezers, that sand
I will be like John Henry
I will finish dead
This tweezers, that sand
I will be like John Henry
I will finish dead