CS 330 Lecture 6 – Regular Expressions
Agenda
- what ?s
- homework 2 deadline
- extracting matches
- capturing
- transforming matches
Program This
- Extract the names of all public methods in a Java source file.
- You’re making an HTML page and have decided to move all images to a directory named
imgs
. How can you update all the links inimg
anda
tags to PNGs and JPGs to reflect the images’ change of location?
Code
chapterer.rb
#!/usr/bin/env ruby
text = IO.read('/Users/johnch/Desktop/prodigal.txt')
text.scan(/CHAPTER (\w+)\s+(.*)/) do
puts "Chapter #{$1}. #{$2}"
end
ocracy.rb
#!/usr/bin/env ruby
words = File.readlines('/usr/share/dict/words')
puts words.grep(/ocracy$/)
blogify.rb
#!/usr/bin/env ruby
text = IO.read(ARGV[0])
# text = text.gsub(/>/, '>')
text.gsub!(/&/, '&')
text.gsub!(/(>)/, '\1>') # Don't really reinclude the >
text.gsub!(/</, '<')
text.gsub!(/ /, ' ')
puts text
report_generator.rb
#!/usr/bin/env ruby
text = IO.read('report_with_equations.txt')
text.gsub!(/{{{(.*?)}}}/) do
eval($1)
end
puts text
report_with_equations.txt
Together we have put {{{(10 * 2) * 365 * 24}}} person-hours {{{5}}} into
our marriage.
Actually, you fell in love with me three years before I fell in
love with you, so really it's about {{{(10 + 13) * 365 * 24}}}.
Haiku
Dear America
We prefer JSON
But we’ll take what we can get
We know our regex
We prefer JSON
But we’ll take what we can get
We know our regex