Regexp.new with Multiple Modifier

Posted by Peter Wagenet Tue, 18 Nov 2008 16:31:00 GMT

This may be a no brainer, but I was working with Regexp.new and ran into and wanted to make a regexp with multiple modifiers. It’s easy enough to make one, the docs show you how to:

r = Regexp.new('dog', Regexp::EXTENDED)   #=> /dog/x

But what if you want more than one modifier? The docs say they should be “or-ed” together. This is as simple as:

r = Regexp.new('test', Regexp::IGNORECASE | Regexp::MULTILINE)
#=> /test/mi

You can even use all three as such:

r = Regexp.new('test', Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED)
#=> /dog/mix

Sort of a no-brainer once you realize what it means.

Comments

Leave a comment

Comments