C# 不区分大小写的Regex,不使用RegexOptions枚举

C# 不区分大小写的Regex,不使用RegexOptions枚举,c#,regex,C#,Regex,是否可以在C#中使用Regex类进行不区分大小写的匹配,而不设置RegexOptions.IgnoreCase标志 我希望能够在正则表达式中定义我是否希望匹配操作以不区分大小写的方式完成 我希望这个正则表达式,taylor,与以下值匹配: 泰勒 泰勒 泰勒 (?i)taylor匹配我指定的所有输入,而无需设置RegexOptions.IgnoreCase标志。 要强制区分大小写,我可以执行(?-I)taylor 看起来其他选项包括: i,不区分大小写 s,单线模式 m,多行模式 x,自由间

是否可以在C#中使用Regex类进行不区分大小写的匹配,而不设置RegexOptions.IgnoreCase标志

我希望能够在正则表达式中定义我是否希望匹配操作以不区分大小写的方式完成

我希望这个正则表达式,
taylor
,与以下值匹配:

  • 泰勒
  • 泰勒
  • 泰勒

(?i)taylor
匹配我指定的所有输入,而无需设置RegexOptions.IgnoreCase标志。

要强制区分大小写,我可以执行
(?-I)taylor

看起来其他选项包括:

  • i
    ,不区分大小写
  • s
    ,单线模式
  • m
    ,多行模式
  • x
    ,自由间距模式

正如勺子16所说,这是
(?i)
。MSDN有一个列表,其中包括一个仅对部分匹配使用不区分大小写匹配的示例:

 string pattern = @"\b(?i:t)he\w*\b";
这里的“t”是不区分大小写的匹配,但其余的是区分大小写的。如果不指定子表达式,将为封闭组的其余部分设置该选项

例如,您可以:

string pattern = @"My name is (?i:taylor).";

这将匹配“我的名字是泰勒”,但不匹配“我的名字是泰勒”。

正如您已经发现的,
(?i)
RegexOptions.IgnoreCase
的内联等价物

仅供参考,您可以使用它执行以下几项操作:

Regex:
    a(?i)bc
Matches:
    a       # match the character 'a'
    (?i)    # enable case insensitive matching
    b       # match the character 'b' or 'B'
    c       # match the character 'c' or 'C'

Regex:
    a(?i)b(?-i)c
Matches:
    a        # match the character 'a'
    (?i)     # enable case insensitive matching
    b        # match the character 'b' or 'B'
    (?-i)    # disable case insensitive matching
    c        # match the character 'c'

Regex:    
    a(?i:b)c
Matches:
    a       # match the character 'a'
    (?i:    # start non-capture group 1 and enable case insensitive matching
      b     #   match the character 'b' or 'B'
    )       # end non-capture group 1
    c       # match the character 'c'
您甚至可以这样组合标志:
a(?mi-s)bc
意思是:

a          # match the character 'a'
(?mi-s)    # enable multi-line option, case insensitive matching and disable dot-all option
b          # match the character 'b' or 'B'
c          # match the character 'c' or 'C'