teaching machines

CS 330 Lecture 8 – Regex Closeout

February 11, 2015 by . Filed under cs330, lectures, spring 2015.

Agenda

TODO

Right now:

Before Friday:

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