所以现在我正在使用这个简单的方法来检查另一个字符串的字符串:
System.Text.RegularExpressions.Regex.IsMatch(toSearch, toVerifyisPresent, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
现在,它的大部分工作都很好。但我最大的问题是,如果我试图搜索“areyou + present”之类的东西,如果“isyou + present”在那里,那么它仍然会变回虚假。我正在考虑因为字符串中的“+”。
我可以做些什么来解决这个问题?
答案 0 :(得分:3)
您可以使用\
转义特殊字符。但正如Oded所指出的那样,如果你只是检查字符串是否包含某些内容,那么最好使用String.Contains
方法。
正则表达式中的特殊字符:
http://www.regular-expressions.info/characters.html
String.Contains方法:
答案 1 :(得分:2)
基于Oded上面的评论。
toSearch.toLowerCase().Contains(toVerifyIsPresent.toLowerCase())
将两者都转换为小写将提供与使用IgnoreCase
答案 2 :(得分:1)
在正则表达式+
中匹配前一个组一次或多次,因此正则表达式areyou+present
匹配:
areyoupresent
areyouupresent
areyouuuuuuuuuuuuuuuuuuuuuuuuuuupresent
等...
答案 3 :(得分:1)
IronPython中的演示:
>>> from System.Text.RegularExpressions import *
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", "areyou+present");
False
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", "areyou\\+present");
True
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", "areyou[+]present");
True
>>> Regex.IsMatch("This is a sentence containing 'areyou+present'", Regex.Escape("areyou+present"));
True