加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 关于正则表达式中的问号

发布时间:2020-12-13 22:55:47 所属栏目:百科 来源:网络整理
导读:我看到一个正则表达式(?i).那么当我们把一个问号放在一个人物面前的意思是什么? 一般来说,这并不意味着什么,甚至可能导致错误(如果问号不符合有效的字符).但是有一些字符确实有效果,即如果这个字符也用作 modifier. regular-expressions.info说到这个特殊
我看到一个正则表达式(?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.

?我意味着这些字符之后的所有内容都应该与大小写不匹配.

另请注意,正如文中所述,并不是所有的正则表达式都支持这种语法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读