Create a case-insensitive regular expression from a string in Ruby
Solution 1
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")
Solution 2
Ruby regexes can interpolate expressions in the same way that strings do, using the #{}
notation. However, you do have to escape any regex special characters. For example:
input_str = "A man + a plan * a canal : Panama!"
/#{Regexp.escape input_str}/i
Solution 3
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.
Solution 4
A slightly more syntactic-sugary way to do this is to use the %r
notation for Regexp literals:
input_str = "A man + a plan * a canal : Panama!"
%r(#{Regexp.escape(input_str)})i
Of course it comes down to personal preference.
Trevor Burnham
Developer at HubSpot. Author of CoffeeScript: Accelerated JavaScript Development, Async JavaScript, and The npm Book.
Updated on November 18, 2020Comments
-
Trevor Burnham over 3 years
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 andRegexp::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.
-
Trevor Burnham over 13 yearsRight, 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 findA+b
,a+B
, orA+B
, notaaaaab
. -
Trevor Burnham over 13 yearsThanks, 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.)
-
Steve Midgley over 9 yearsThis is such a great answer - it reads way better than the top voted response and feels more like Ruby to me to boot. Hope others will upvote this too..
-
user2066657 almost 5 yearsI can't unify what you wrote just above with your original question. Consider revising your question?
-
cdpalmer almost 3 yearsI agree, I just upvoted this answer that now ties the accepted answer! One more vote!
-
Ahmed Kooli over 2 yearsUsed this in a clash of code competition :D