2012-08-12 104 views
1

大家好:我需要使用RexEx匹配相似的单词。例如,如果我有一个包含“自主”这样的词的模式,它应该匹配单词“自主”而不匹配“自主”。
示例代码:
使用RegEx匹配相似单词

void modify(string word) 
{ 
string input = "This island is a colony; however,it is autonomous and " + 
"receives no orders from the mother country, autonomy, N."; 
string pattern = @",\s" + word + @"\s[v|n|adj]\.";//word = "autonomous"; 
Regex reg = new Regex(pattern); 
string output = reg.Replace(input, "."); 
} 
+0

对不起。我向女士道歉。 – FadelMS 2012-08-12 05:13:46

+0

你确定你的代码与自治系统匹配吗? – 2012-08-12 05:22:52

+0

@habib:那是我不想要的。 – FadelMS 2012-08-12 05:28:46

回答

1

这可能是你在找什么:

string s= "This island is a colony; however,it is autonomous and receives no orders from the mother country, autonomy, N.";; 
string pattern="autonomous"; 
Regex r=new Regex(@"\b(?!"+pattern+")"+pattern.Substring(0,pattern.Length/2)[email protected]".*?\b"); 
r.Replace(s,"."); 
1

我不知道你将能够轻松地单独用正则表达式实现。

你应该看看模式匹配算法。有a similar question here涵盖此主题。