2013-09-29 34 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"; 
相关问题