我目前拥有的正则表达式代码将会区分大小写地精确匹配,那么我需要做出什么改变才能忽略大小写呢?
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|...
"RegexOption.IgnoreCase 应该是一个选项。
Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)), RegexOptions.IgnoreCase)
只需使用 Regex.IsMatch
的重载,它允许您指定选项:
return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)), RegexOptions.IgnoreCase);
\w\W
或\W\w
为真的位置。请参阅单词边界。 - JDB