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.
/i stands for ignore case in the given string. Usually referred to as case-insensitive as pointed out in the comment.
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.
\\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.
2. What is the significance of $ used in regular expression in UNIX? Explanation: Regular expression provides more flexibility while matching string patterns.
(?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.
(?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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With