C# RegEx:忽略大小写...在模式中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1583678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:03:14  来源:igfitidea点击:

C# RegEx: Ignore case... in pattern?

c#regex

提问by core

I'm using System.Text.RegularExpressions.Regex.IsMatch(testString, regexPattern) to do some searches in strings.

我正在使用 System.Text.RegularExpressions.Regex.IsMatch(testString, regexPattern) 在字符串中进行一些搜索。

Is there a way to specify in the regexPattern string that the pattern should ignore case? (I.e. without using Regex.IsMatch(testString, regexPattern, RegexOptions.IgnoreCase))

有没有办法在 regexPattern 字符串中指定模式应该忽略大小写?(即不使用 Regex.IsMatch(testString, regexPattern, RegexOptions.IgnoreCase))

采纳答案by stevemegson

(?i)within the pattern begins case-insensitive matching, (?-i)ends it. That is,

(?i)在模式中开始不区分大小写的匹配,(?-i)结束它。那是,

(?i)foo(?-i)bar

matches FOObarbut not fooBAR.

匹配FOObar但不匹配fooBAR

EDIT:I should have said (?-i)starts case-sensitive matching - if you want the whole pattern to be case-insensitive then you don't need to "end" the (?i).

编辑:我应该说(?-i)开始区分大小写匹配 - 如果您希望整个模式不区分大小写,那么您不需要“结束” (?i).