0

I have run into a problem when trying to find multiple matches in a length of text. The text has the following format:

string text = "#IDENTIFIER http://www.link1.com #IDENTIFIER http://www.link2.org #IDENTIFIER http://www.link3.com #IDENTIFIER http://www.link4.net";

My objective is to extract each #IDENTIFIER link occurrence from this and I am doing it using the following code:

string pat = @"(#IDENTIFIER)(.*)\.(com|org|net)";
MatchCollection matches = Regex.Matches(text, pat);
foreach(Match match in matches) {
    Console.WriteLine("'{0}' found at index {1}.", match.Value, match.Index);
}

The problem is, it returns one match and not 4. Why are the in-between patterns ignored?

Do you know what I am missing?

dspfnder
  • 1,135
  • 1
  • 8
  • 13

1 Answers1

1

It's because .* on its own is generally greedy. Instead try using .*?:

string pat = @"(#IDENTIFIER)(.*?)\.(com|org|net)";
Lloyd
  • 29,197
  • 4
  • 84
  • 98