I saw a regular expression says (?i)
. So what does it mean when we put a question mark in front of a character?
我看到一个正则表达式(?i)。那么当我们在角色前面放一个问号时,它意味着什么?
1 个解决方案
#1
11
In general it does not mean anything and might even result in an error (if the question mark does not follow a valid character). But there are certain characters where it does have an effect, namely if this character is also used as modifier.
一般来说,它并不意味着什么,甚至可能导致错误(如果问号不遵循有效字符)。但是某些字符确实有效,即如果此字符也用作修饰符。
regular-expressions.info says about this particular syntax:
regular-expressions.info说明了这个特殊的语法:
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.现代正则表达式风格允许您仅将修饰符应用于正则表达式的一部分。如果在正则表达式的中间插入修饰符(?ism),则修饰符仅适用于修饰符右侧的正则表达式部分。您可以通过在前面加上减号来关闭模式。减号后的所有模式都将关闭。例如。 (?i-sm)打开不区分大小写,并关闭单线模式和多线模式。
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.并非所有正则表达式都支持这一点。 JavaScript和Python将所有模式修饰符应用于整个正则表达式。它们不支持(?-ismx)语法,因为当模式修饰符应用于整个正则表达式时,关闭选项是没有意义的。默认情况下,所有选项均已关闭
You can quickly test how the regex flavor you're using handles mode modifiers. The regex
(?i)te(?-i)st
should matchtest
andTEst
, but notteST
orTEST
.您可以快速测试正在使用的正则表达式处理模式修饰符的方式。正则表达式(?i)te(? - i)st应匹配test和TEst,但不匹配teST或TEST。
?i
means that everything following these characters should be matched case-insensitive.
?i表示跟随这些字符的所有内容都应该不区分大小写。
Also note that, as the text says, not all regex flavors support this syntax.
另请注意,正如文中所说,并非所有正则表达式都支持此语法。
#1
11
In general it does not mean anything and might even result in an error (if the question mark does not follow a valid character). But there are certain characters where it does have an effect, namely if this character is also used as modifier.
一般来说,它并不意味着什么,甚至可能导致错误(如果问号不遵循有效字符)。但是某些字符确实有效,即如果此字符也用作修饰符。
regular-expressions.info says about this particular syntax:
regular-expressions.info说明了这个特殊的语法:
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.现代正则表达式风格允许您仅将修饰符应用于正则表达式的一部分。如果在正则表达式的中间插入修饰符(?ism),则修饰符仅适用于修饰符右侧的正则表达式部分。您可以通过在前面加上减号来关闭模式。减号后的所有模式都将关闭。例如。 (?i-sm)打开不区分大小写,并关闭单线模式和多线模式。
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.并非所有正则表达式都支持这一点。 JavaScript和Python将所有模式修饰符应用于整个正则表达式。它们不支持(?-ismx)语法,因为当模式修饰符应用于整个正则表达式时,关闭选项是没有意义的。默认情况下,所有选项均已关闭
You can quickly test how the regex flavor you're using handles mode modifiers. The regex
(?i)te(?-i)st
should matchtest
andTEst
, but notteST
orTEST
.您可以快速测试正在使用的正则表达式处理模式修饰符的方式。正则表达式(?i)te(? - i)st应匹配test和TEst,但不匹配teST或TEST。
?i
means that everything following these characters should be matched case-insensitive.
?i表示跟随这些字符的所有内容都应该不区分大小写。
Also note that, as the text says, not all regex flavors support this syntax.
另请注意,正如文中所说,并非所有正则表达式都支持此语法。