teaching machines

CS 330 Lecture 6 – Regular Expressions

February 3, 2014 by . Filed under cs330, lectures, spring 2014.

Agenda

Program This

  1. Extract the names of all public methods in a Java source file.
  2. 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 in img and a 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!(/</, '&lt;')
text.gsub!(/  /, '&nbsp;&nbsp;')

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