stevengharms.com

Sententiae viri ex temporibus duobus

Ruby Backreference Pattern Matching for the Brain-Dead

[code lang="ruby"]
irb(main):001:0> "capture to here!  leave this off".gsub(/(.*\!).*(\s+.*)$/, "\\1 and \\2")
=> "capture to here! and  off"
[/code]

I looked in my Pickaxe, I looked in various tutorials, but this simple little thing hung me up more than many other Ruby questions I’ve had over the years.

Comments