Regular Expressions

In this episode of the Weekly Iteration, Chris is joined by Matthew Mongeau, aka Goose, to discuss regular expressions. Learn how to work with them, where they fit, and perhaps more importantly where they don't. Regex Introduction If you're new ...
This is a companion discussion topic for the original entry at

Great video!

I like this alternative to gsub with a block:

substitutions = {
  "world" => "planet",
  "test" => "experiment",
  "a" => "an"

pattern = substitutions.keys.join('|')

sample = "Hello world, this is a test string."
puts sample.gsub!(pattern, substitutions) 
#=> "Hello planet, this is an experiment string."

It’s useful when only a simple replacement is needed for each matched term (h/t to Avdi Grimm for introducing me to it)

the cheat sheet geocities link is broken

Oh no! It was bound to happen eventually, but that still didn’t prepare me… :frowning:

Wayback Machine was unsuccessful in locating a cached version. The king is dead.

Crisis averted! We found a copy and I’ve updated the link to point at the local copy (for the Oniguruma cheat sheet). Close call.

1 Like