Flashback to December 2012!
SIGITE 2012

teaching machines

CS 330 Lecture 5 – Regex

Dear students,

Let’s dive right into some Ruby scripts:

We aren’t going to concentrate on Ruby as a programming language much at the moment. What we are more concerned with at present are regular expressions. Regex is a language for describing languages. Regexes are used to build syntax highlighters and interpreters and compilers. They excel at helping you clean up noisy text data. They are used to ensure that user input conforms to certain syntactic rules. They turn grunt work into intellectual activity.

Let’s break down the pieces of regular expressions into three categories:

We will sift our fingers through regex by classifying some common expressions under these categories:

click for a random term...

Here’s your TODO list for next time:

Sincerely,

aspect.rb

#!/usr/bin/env ruby

aspect = ARGV[0].to_f / ARGV[1].to_f
puts aspect

santa.rb

#!/usr/bin/env ruby

text = File.read ARGV[0]

nups = text.count '('
ndowns = text.count ')'

puts nups - ndowns

rhymes.rb

#!/usr/bin/env ruby

require 'json'
require 'open-uri'

url = "http://rhymebrain.com/talk?function=getRhymes&word=#{ARGV[0]}"

json = open(url).read
rhymes = JSON.parse(json)

rhymes.each do |rhyme|
  puts rhyme['word'] if rhyme['score'] >= 300
  if rhyme['score'] >= 300; puts rhyme['word']; end
end

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Past Posts

Categories