CS 330 Lecture 6 – Regex in Ruby
Agenda
- what ?s
- visualizing regex with Regexper
- asserting
- filtering with scan, grep, etc.
- substitutions
TODO
- Start the Regexercise homework. At least the first two problems must be completed before February 20. Remember to follow the Bitbucket workflow. I encourage you to commit and push frequently—do not wait until you’ve got everything working.
Code
isa.rb
#!/usr/bin/env ruby
path = ARGV[0].downcase
if path =~ /\.(jpeg|jpg)$/
puts 'You jpeg.'
end
nofoo.rb
#!/usr/bin/env ruby
text = IO.read(ARGV[0])
if text !~ /foo/
exit 0
else
exit 1
end
nocomments.rb
#!/usr/bin/env ruby
#
# this is a comment
File.readlines(ARGV[0]).each do |line| # asdfasd
if line !~ /^\s*#/
puts line
end
end
ocracy.rb
#!/usr/bin/env ruby
lines = File.readlines('/usr/share/dict/words')
puts lines.grep(/(ocracy|archy)$/)
Haiku
on fuzzy matching
I lost my puppy
Sadly I had no photos
So I hung no signs
I lost my puppy
Sadly I had no photos
So I hung no signs