Saturday, May 15, 2010

Regex riff (for words not containing a string)

I had the need to figure out how to parse a string and pull out any words that didn't contain "Foo" in them. After a lot of cursing, I came up with something that works. For the string "Which BarFoo Is It Foo Bar":

/\b((?![\w]*Foo)\w+)\b/
view raw snippet.rb hosted with ❤ by GitHub

Yields: "Which", "Is", "It", "Bar"

Regular expressions are diabolical.