2023-08-23 07:00:04

Regex:忽略大小写

如何使下面的正则表达式忽略大小写敏感性?它应该匹配所有正确的字符,但忽略它们是小写还是大写。

G[a-b].*

当前回答

(gG) [aAbB]。*如果图案不是太复杂或太长,可能是简单的解决方案。

其他回答

科特林:

"G[a-b].*".toRegex(RegexOption.IGNORE_CASE)

在Java中,正则表达式构造函数具有

Regex(String pattern, RegexOption option)

要忽略大小写,请使用

option = RegexOption.IGNORE_CASE

只是为了完整起见,我想在Unicode中添加正则表达式的解决方案:

std::tr1::wregex pattern(szPattern, std::tr1::regex_constants::icase);

if (std::tr1::regex_match(szString, pattern))
{
...
}

你可以使用查找/替换在Visual Studio和Visual Studio Code中练习Regex。

对于带大小写的正则表达式,需要同时选择“匹配大小写”和“正则表达式”。否则[A-Z]行不通。在这里输入图像描述

C#

using System.Text.RegularExpressions;
...    
Regex.Match(
    input: "Check This String",
    pattern: "Regex Pattern",
    options: RegexOptions.IgnoreCase)

options: RegexOptions。IgnoreCase