2023-08-23 07:00:04

Regex:忽略大小写

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

G[a-b].*

当前回答

科特林:

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

其他回答

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

Regex(String pattern, RegexOption option)

要忽略大小写,请使用

option = RegexOption.IGNORE_CASE

C#

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

options: RegexOptions。IgnoreCase

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

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

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

i标志通常用于区分大小写。这里没有给出具体的语言,但可能是/G[ab]之类的。*/i or /(?i)G[ab].*/。

正如我从这篇类似的文章中发现的那样(在AWK中ignorecase),在老版本的AWK上(比如在普通的Mac OS X上),你可能需要使用'tolower($0) ~ /pattern/'。

IGNORECASE或(?i)或/pattern/i将为每一行生成一个错误或返回true。