Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do `?i` and `?-i` in regex mean?

Tags:

regex

ruby

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.

like image 955
Luccas Avatar asked Feb 28 '13 21:02

Luccas


People also ask

What does I in regex mean?

/i stands for ignore case in the given string. Usually referred to as case-insensitive as pointed out in the comment.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What does this mean in regex \\ s *?

\\s*,\\s* It says zero or more occurrence of whitespace characters, followed by a comma and then followed by zero or more occurrence of whitespace characters. These are called short hand expressions. You can find similar regex in this site: http://www.regular-expressions.info/shorthand.html.

What is the significance of (? I used in regular expression in Unix?

2. What is the significance of $ used in regular expression in UNIX? Explanation: Regular expression provides more flexibility while matching string patterns.


3 Answers

(?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.

like image 156
gpojd Avatar answered Oct 20 '22 18:10

gpojd


(?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.

like image 21
Mathletics Avatar answered Oct 20 '22 17:10

Mathletics


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.

like image 10
gmaliar Avatar answered Oct 20 '22 17:10

gmaliar