gpt4 book ai didi

regex - 关于正则表达式中的问号

转载 作者:行者123 更新时间:2023-12-04 02:06:02 26 4
gpt4 key购买 nike

我看到一个正则表达式说 (?i) .那么当我们在一个字符前加上一个问号是什么意思呢?

最佳答案

一般来说,它没有任何意义,甚至可能导致错误(如果问号后面没有有效字符)。但是有些字符确实有效果,即如果该字符也用作 modifier .
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.

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意味着这些字符后面的所有内容都应该不区分大小写进行匹配。
另请注意,正如文中所说,并非所有正则表达式都支持此语法。

关于regex - 关于正则表达式中的问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7935319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com