top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible to make a half part of regex as case-insensitive?

+1 vote
306 views
Is it possible to make a half part of regex as case-insensitive?
posted May 21, 2014 by Anuradha Tabyal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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. If you insert the modifier (?ism) in the middle of the regex, the modifier only applies to the part of the regex to the right of the modifier. You can turn off modes by preceding them with a minus sign. All modes after the minus sign will be turned off. E.g. (?i-sm) turns on case insensitivity, and turns off both single-line mode and multi-line mode.

Not all regex flavors support this. JavaScript and Python apply all mode modifiers to the entire regular expression. They don't support the (?-ismx) syntax, since turning off an option is pointless when mode modifiers apply to the whole regular expressions. All options are off by default.

You can quickly test how the regex flavor you're using handles mode modifiers. The regex (?i)te(?-i)st should match test and TEst, but not teST or TEST.

Source: http://www.regular-expressions.info/modifiers.html

answer May 22, 2014 by Udita
Similar Questions
+1 vote

I have about 500 + ip regex in my apache file like this

99.45.3[2-9]..*$ 
^99.45.4[0-9]..*$ 
^99.45.5[0-9]..*$ 

I have to convert like this into CIDR .

99.45.32.0/21
99.45.40.0/21
99.45.48.0/23
99.45.50.0/21
99.45.58.0/23

Is there a module that can help me convert ip regex to ip range .

+4 votes

Probably I'm turning the use of regular expressions upside down with this question. I don't want to write a regex that matches prefixes of other strings, I know how to do that. I want to generate a regex -- given another regex --, that matches all possible strings that are a prefix of a string that matches the given regex.

E.g. You have the regex ^[a-z]*4R$ then the strings "a", "ab", "A4" "ab4" are prefixes of this regex (because there is a way of adding characters that causes the regex to match), but "4a" or "a44" or not.
How do I programmatically create a regex that matches "a", "ab", "A4", etc.. but not "4a", "a44", etc..

Logically, I'd think it should be possible by running the input string against the state machine that the given regex describes, and if at some point all the input characters are consumed, it's a match. (We don't have to run the regex until the end.) But I cannot find any library that does it...

...