2013-09-29 56 views
0

我需要一般說明來搜索文本中的任何非字母字符之前和之後的句子或單個單詞.. 我會給幾個例子來說明我需要什麼:C#正則表達式,可能包含任何非字母字符的句子

搜索「好運氣」:

good luck in the exam.. (should match the pattern) 
hello! good luck in the exam.. (should match) 
mm,good luck! .. (should match) 
hello again i wish yougood luckin the exam. (**should Not match**) 

我指的是正則表達式應該匹配的句子時,它不與字母字符包圍。

回答

1
String [email protected]"\b"[email protected]"\b"; 

\b是一個非字boundary..Simply地說,它會讓你滿足不同的話,即一個詞是不是另一個字


注意你應該逃脫輸入的一部分,因爲如果你的輸入包含字符如*,? ..它將被視爲正則表達式中的特殊字符。所以,它應該是

String [email protected]"\b"+Regex.Escape(input)[email protected]"\b"; 
相關問題