1

Hi all: I need to match similar words using RexEx. For example, if I have a pattern which contains a word like "autonomous", it should match the word "autonomy" without matching "autonomous".
Example Code:

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, ".");
}
Anirudha
  • 32,393
  • 7
  • 68
  • 89
FadelMS
  • 2,027
  • 5
  • 25
  • 42

2 Answers2

1

I'm not sure you're going to be able to easily achieve with a Regex alone.

You should take a look at pattern matching algorithms. There's a similar question here that covers this topic.

Community
  • 1
  • 1
jstr
  • 1,271
  • 10
  • 17
1

This may be what you are looking for:

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)+@".*?\b");
r.Replace(s,".");
Anirudha
  • 32,393
  • 7
  • 68
  • 89