teaching machines

CS 330 Lecture 5 – Advanced Regex

February 1, 2013 by . Filed under cs330, lectures, spring 2013.

Agenda

TODO

Code

states.txt

Alabama - Montgomery
Alaska - Juneau
Arizona - Phoenix
Arkansas - Little Rock
California - Sacramento
Colorado - Denver
Connecticut - Hartford
Delaware - Dover
Florida - Tallahassee
Georgia - Atlanta
Hawaii - Honolulu
Idaho - Boise
Illinois - Springfield
Indiana - Indianapolis
Iowa - Des Moines
Kansas - Topeka
Kentucky - Frankfort
Louisiana - Baton Rouge
Maine - Augusta
Maryland - Annapolis
Massachusetts - Boston
Michigan - Lansing
Minnesota - St. Paul
Mississippi - Jackson
Missouri - Jefferson City
Montana - Helena
Nebraska - Lincoln
Nevada - Carson City
New Hampshire - Concord
New Jersey - Trenton
New Mexico - Santa Fe
New York - Albany
North Carolina - Raleigh
North Dakota - Bismarck
Ohio - Columbus
Oklahoma - Oklahoma City
Oregon - Salem
Pennsylvania - Harrisburg
Rhode Island - Providence
South Carolina - Columbia
South Dakota - Pierre
Tennessee - Nashville
Texas - Austin
Utah - Salt Lake City
Vermont - Montpelier
Virginia - Richmond
Washington - Olympia
West Virginia - Charleston
Wisconsin - Madison
Wyoming - Cheyenne

instatiate.rb

#!/usr/bin/env ruby

text = IO.read(ARGV[0])
text = text.gsub(/.*(North|West|South|New|Island).*/, '')
text = text.gsub(/^([A-Z].*) - (.*)$/, 'new State("\\1", "\\2"),')
# puts $1 
# puts $2 
puts text

showext.rb

#!/usr/bin/env ruby

entries = Dir.entries(ARGV[0])

entries.each do |entry|
  if entry =~ /\.rb$/
    puts entry
  end
end

State.java

public class State {

  public State(String name, String capital) {
  }

  public String getName() {
  }

  public String getCapital() {
  }

  private void secede() {
  } 

  public void blap() {
  }
}

javadocify.rb

#!/usr/bin/env ruby

text = IO.read(ARGV[0])
text = text.gsub(/^(?=  public )/, "  /**\n   *\n   */\n")
puts text

foo.txt

My favorite number is {{{1 + 3}}}.

eval.rb

#!/usr/bin/env ruby

text = IO.read(ARGV[0])
text = text.gsub(/\{\{\{(.*?)\}\}\}/) do
  $stderr.puts "Evaluating #{$1}"
  eval($1)
end
puts text

Haiku

He went foot to foot.
The slipper fit just the last.
Next time: $.