我所經歷的這個問題 C#, Regex.Match whole words正則表達式匹配整個單詞與特殊字符不工作?
它說的全字匹配使用「\ bpattern \ B」 這工作正常全字匹配,沒有任何特殊字符,因爲它是爲隻字字符!
我需要一個表達式來匹配帶有特殊字符的單詞。我的代碼如下
class Program
{
static void Main(string[] args)
{
string str = Regex.Escape("Hi temp% dkfsfdf hi");
string pattern = Regex.Escape("temp%");
var matches = Regex.Matches(str, "\\b" + pattern + "\\b" , RegexOptions.IgnoreCase);
int count = matches.Count;
}
}
但它由於%失敗。我們有任何解決方法嗎? 可以有其它特殊字符,如「空間」,「(」,「)」等
是的,但不是他的問題(僅)的原因。 –