CS 330 Lecture 5 – Regex
Dear students,
Let’s dive right into some Ruby scripts:
- Calculate an aspect ratio from dimensions given as command line parameters.
- Generate a list of words that rhyme with a word given as a command-line parameter.
- Solve Advent of Code problem 1.
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:
- the elements, or what to match
- the quantifiers, or how many to match
- the anchors, or where to match
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:
- Start the Regexercise homework. Due before February 15.
- Run
git pull template master
to pull down some tweaks to the Regexercise PDF. - Watch for a link to quiz 2 in your email.
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