正则表达式中的`?i` 和`?-i` 是什么意思? [英] What do `?i` and `?-i` in regex mean?

查看:163
本文介绍了正则表达式中的`?i` 和`?-i` 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释一下 (?i)(?-i) 在正则表达式中包裹一个词是什么意思吗?

Could someone explain what (?i) and (?-i) wrapping a word in regex mean?

(?i)test(?-i)

我测试过,它匹配 testTESTteSt.但我以前从未见过这种情况.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) 启动不区分大小写模式

(?i) starts case-insensitive mode

(?-i) 关闭不区分大小写模式

(?-i) turns off case-insensitive mode

在此页面的仅为部分正则表达式打开和关闭模式"部分的更多信息:

现代正则表达式风格允许您仅将修饰符应用于部分正则表达式.如果在中间插入修饰符 (?ism)正则表达式,修饰符只适用于正则表达式的一部分修饰符的右侧.您可以通过在它们前面加上一个来关闭模式减号.减号后的所有模式都将关闭.例如.(?i-sm) 打开不区分大小写,并关闭两个单行模式和多行模式.

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.

并非所有正则表达式都支持这一点.JavaScript 和 Python 都适用整个正则表达式的模式修饰符.他们不支持(?-ismx) 语法,因为在以下情况下关闭选项毫无意义模式修饰符适用于整个正则表达式.所有选项都是默认关闭.

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.

您可以快速测试您使用的正则表达式风格如何处理句柄模式修饰符.正则表达式 (?i)te(?-i)st 应该匹配 test 和 TEst,但不匹配测试或测试.

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` 和`?-i` 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆