正则表达式:忽略大小写敏感 [英] Regex: ignore case sensitivity
问题描述
如何使以下正则表达式忽略大小写敏感?它应该匹配所有正确的字符,但忽略它们是小写还是大写.
How can I make the following regex ignore case sensitivity? It should match all the correct characters but ignore whether they are lower or uppercase.
G[a-b].*
推荐答案
假设您希望 整个 正则表达式忽略大小写,您应该寻找 i
标志.几乎所有的正则表达式引擎都支持它:
Assuming you want the whole regex to ignore case, you should look for the i
flag. Nearly all regex engines support it:
/G[a-b].*/i
string.match("G[a-b].*", "i")
查看您的语言/平台/工具的文档,了解如何指定匹配模式.
Check the documentation for your language/platform/tool to find how the matching modes are specified.
如果您只希望正则表达式的部分不区分大小写(正如我原来的答案所假定的那样),那么您有两个选择:
If you want only part of the regex to be case insensitive (as my original answer presumed), then you have two options:
使用
(?i)
和 [可选](?-i)
模式修饰符:
Use the
(?i)
and [optionally](?-i)
mode modifiers:
(?i)G[a-b](?-i).*
将所有变体(即小写和大写)放入正则表达式 - 如果不支持模式修饰符,则很有用:
Put all the variations (i.e. lowercase and uppercase) in the regex - useful if mode modifiers are not supported:
[gG][a-bA-B].*
最后一点:如果您正在处理除 ASCII 之外的 Unicode 字符,请检查您的正则表达式引擎是否正确支持它们.
One last note: if you're dealing with Unicode characters besides ASCII, check whether or not your regex engine properly supports them.
这篇关于正则表达式:忽略大小写敏感的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!