What do `?i` and `?-i` in regex mean?
RubyRegexRuby Problem Overview
Could someone explain what (?i)
and (?-i)
wrapping a word in regex mean?
(?i)test(?-i)
I tested and it matches test
, TEST
, and teSt
. But I have never seen this before. What does the ?
before i
mean? I saw this here.
Ruby Solutions
Solution 1 - Ruby
(?i)
starts case-insensitive mode
(?-i)
turns off case-insensitive mode
More information at the "Turning Modes On and Off for Only Part of The Regular Expression" section of this page:
> 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.
Solution 2 - Ruby
(?i)
turns on case-insensitive mode, (?-i)
turns it off.
For example, if you tried (?i)te(?-i)st
, it would match test, TEst, tEst, but not teST.
Solution 3 - Ruby
Taken directly from ruby docs.
> The end delimiter for a regexp can be followed by one or more > single-letter options which control how the pattern can match. > > > /pat/i - Ignore case > > /pat/m - Treat a newline as a character matched by . > > /pat/x - Ignore whitespace and comments in the pattern > > /pat/o -> Perform #{} interpolation only once > > i, m, and x can also be applied on > the subexpression level with the (?on-off) construct, which enables > options on, and disables options off for the expression enclosed by > the parentheses.