Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a case-insensitive regular expression from a string in Ruby

Tags:

regex

ruby

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.

like image 601
Trevor Burnham Avatar asked Aug 06 '10 17:08

Trevor Burnham


People also ask

How do you make a string case insensitive in regex?

To enable the regex case insensitive matching, add (?) prefix or enable the case insensitive flag directly in the Pattern. compile() .

What does =~ mean in Ruby regex?

=~ is Ruby's pattern-matching operator. It matches a regular expression on the left to a string on the right. If a match is found, the index of first match in string is returned. If the string cannot be found, nil will be returned.

What kind of regex does Ruby use?

A regular expression is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings. Ruby regular expressions i.e. Ruby regex for short, helps us to find particular patterns inside a string. Two uses of ruby regex are Validation and Parsing.

Are regular expressions case sensitive?

The regular expression pattern is matched in the input string from left to right. Comparisons are case-sensitive. The ^ and $ language elements match the beginning and end of the input string. The end of the input string can be a trailing newline \n character.


2 Answers

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") 
like image 90
sepp2k Avatar answered Sep 23 '22 23:09

sepp2k


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 
like image 42
David Tresner-Kirsch Avatar answered Sep 20 '22 23:09

David Tresner-Kirsch