[英]C# regex matching exact string in a line but ignore case
我目前拥有的正则表达式的代码将寻找与case完全匹配的内容,因此我必须做出哪些更改才能忽略这种情况?
public static bool ExactMatch(string input, string match)
{
return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)));
}
这应该工作:
public static bool ExactMatch(string input, string match)
{
return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)), RegexOptions.IgnoreCase);
}
(?i)
参数使正则表达式不区分大小写:
@"(?i)\b{0}\b"
请注意, \\b
字边界仅在搜索词以字母数字字符开头和结尾时起作用。
可以使用服务器端“ (?i)
”,但这不适用于客户端。 我想它应该适合你,它会忽略这个案子。
即“ ...(?i)(jpg|jpeg|gif|png|wpf|...
”
希望能帮助到你。
只需使用Regex.IsMatch
的重载,它允许您指定选项:
return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)), RegexOptions.IgnoreCase);
RegexOption.IgnoreCase应该是一个选项..
Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)), RegexOptions.IgnoreCase)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.