teaching machines

CS 330 Lecture 5 – Regular Expressions

Agenda

  • what ?s
  • Perry on “what’s best?”
  • what are regices used for?
  • common regex algorithms in Ruby
    • asserting a pattern
    • extracting matches
    • transforming matches
  • languages’ differing tacks on regex

TODO

Perry’s Stages of Intellectual Development

  1. Dualism, in which I believe everything can be clearly labeled right and wrong, good and bad, smart and stupid. Zsh reigns supreme. Bash 5u><!
  2. Multiplicity, in which I believe rightness is just an arbitrary opinion that one holds. The world is too complex to be governed by external authority. Therefore, I must listen to my own feelings. Others’ opinions have no bearing on me. Get your shell out of my face.
  3. Relativism, in which I believe the opinions we hold on what is right and good are not arbitrary but based on reason. Each of us has a background of certain experiences and is progressing toward certain goals, and our background and goals contribute significantly to our beliefs and actions. We recognize that some beliefs and actions are more valid than others. Zsh is best for my workflow and it’s what I know. Bash works best for you, and it’s what you know.
  4. Commitment, in which, having seen many sides of an issue, I align myself with my beliefs but do not silence those who believe otherwise. I view opposition as an opportunity to strengthen or alter my stance on an issue. Zsh is my go-to shell, but I’ve been reading the Bash book you give me. Cool stuff, dude. I’m going to go tweak my Zsh prompt to show that I have uncommitted files just like they did on page 357.

Why Are We Reading?

Charles Spurgeon said, “The man who never reads will never be read; he who never quotes will never be quoted. He who will not use the thoughts of other men’s brains proves that he has no brains of his own.”

Code

isathing.rb

#!/usr/bin/env ruby

if ARGV[0] =~ /\.(jpg|jpeg)$/
  puts 'You got youself a JPEG'
elsif ARGV[0] =~ /\.png$/
  puts 'You got youself a PNG'
else
  puts 'You got youself a mystery'
end

isatime.rb

#!/usr/bin/env ruby

# if ARGV[0] !~ /^[0-1]?[0-9]?:[0-5]\d [AP]M$/ 
if ARGV[0] !~ /^(0?[1-9]|1[012]):[0-5]\d [AP]M$/
  puts 'You ain\'t got youself a time'
end

middless.rb

#!/usr/bin/env ruby

$stdin.each do |line|
  if line !~ /^\S+ \S+ \S+,/
    puts line
    puts 'Your middle name is a mystery'
  end
end

Haiku

Facebook needs regex
s/gun|Fox|Texas/cheese/
Keeps blood pressure down

Comments

Leave a Reply

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