Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is regex case insensitivity slower?

Source

RegexOptions.IgnoreCase is more expensive than I would have thought (eg, should be barely measurable)

Assuming that this applies to PHP, Python, Perl, Ruby etc as well as C# (which is what I assume Jeff was using), how much of a slowdown is it and will I incur a similar penalty with /[a-zA-z]/ as I will with /[a-z]/i ?

like image 956
Teifion Avatar asked Aug 28 '08 10:08

Teifion


People also ask

Is regex case-sensitive?

By default, the comparison of an input string with any literal characters in a regular expression pattern is case-sensitive, white space in a regular expression pattern is interpreted as literal white-space characters, and capturing groups in a regular expression are named implicitly as well as explicitly.

How do you make a section insensitive in a regex case?

Perl lets you make part of your regular expression case-insensitive by using the (? i:) pattern modifier. Modern regex flavors allow you to apply modifiers to only part of the regular expression.

Is JavaScript regex case-sensitive?

Regular expression, or simply RegEx JavaScript allows you to write specific search patterns. You can also make the search case-sensitive or insensitive, search for a single JavaScript RegEx match or multiple, look for characters at the beginning or the end of a word.


1 Answers

Yes, [A-Za-z] will be much faster than setting the RegexOptions.IgnoreCase, largely because of Unicode strings. But it's also much more limiting -- [A-Za-z] does not match accented international characters, it's literally the A-Za-z ASCII set and nothing more.

I don't know if you saw Tim Bray's answer to my message, but it's a good one:

One of the trickiest issues in internationalized search is upper and lower case. This notion of case is limited to languages written in the Latin, Greek, and Cyrillic character sets. English-speakers naturally expect search to be case-insensitive if only because they’re lazy: if Nadia Jones wants to look herself up on Google she’ll probably just type in nadia jones and expect the system to take care of it.

So it’s fairly common for search systems to “normalize” words by converting them all to lower- or upper-case, both for indexing and queries.

The trouble is that the mapping between cases is not always as straightforward as it is in English. For example, the German lower-case character “ß” becomes “SS” when upper-cased, and good old capital “I” when down-cased in Turkish becomes the dotless “ı” (yes, they have “i”, its upper-case version is “İ”). I have read (but not verified first-hand) that the rules for upcasing accented characters such “é” are different in France and Québec. One of the results of all this is that software such as java.String.toLowerCase() tends to run astonishingly slow as it tries to work around all these corner-cases.

http://www.tbray.org/ongoing/When/200x/2003/10/11/SearchI18n

like image 179
Jeff Atwood Avatar answered Oct 06 '22 22:10

Jeff Atwood