tags:

views:

44

answers:

2

Let's say that I have an arbitrary string like

`A man + a plan * a canal : Panama!`

and I want to do a regex search for strings that are the same other than case. That is, this regular expression should match the string

`a man + A PLAN * a canal : PaNaMa!`

I take it the best approach is to backslash-escape every character with a special meaning in Ruby regular expressions, and then do Regexp.new with that string and Regexp::IGNORECASE as arguments. Is that right? Is there a tried-and-true regular expression for converting arbitrary strings into literal regular expressions?

By the way, I ultimately want to use this regular expression to do an arbitrary case-insensitive MongoDB query. So if there's another way I could be doing that, please let me know.

+3  A: 

You can use Regexp.escape to escape all the characters in the string that would otherwise be handled specially by the regexp engine.

Regexp.new(Regexp.escape("A man + a plan * a canal : Panama!"), Regexp::IGNORECASE)

or

Regexp.new(Regexp.escape("A man + a plan * a canal : Panama!"), "i")
sepp2k
Thanks, that was just what I was looking for! (Though as to MongoDB, I realized that if I'm doing this kind of search often, I should really store a downcased version of the string for performance reasons.)
Trevor Burnham
A: 

If you know the regular expression you want already, you can add "i" after the expression (eg /the center cannot hold it is too late/i) to make it case insensitive.

Andrew Grimm
Right, but that's not the question I'm asking. I have an arbitrary string (from user input), not a regular expression. If the user enters `a+b`, for example, I want to be able to find `A+b`, `a+B`, or `A+B`, not `aaaaab`.
Trevor Burnham