3

I need to find matching string that takes minimum length, but my code shows exactly opposite, how can I fixed it?

string line = @"xxxxxxxxxxxx A(a,b) and A(a,b) > 0 xxxxxxxxx \n xxxxxxxxx A(a,b) and A(a,b) > 0 xxxxxxxxxxxxxxxxxx";
string Pattern = "A.+?>";
MatchCollection M1 = Regex.Matches(line, Pattern);
Console.WriteLine(M1[0].ToString()); //I want 【A(a,b) >】 but it shows 【A(a,b) and A(a,b) >】
Console.WriteLine(M1[1].ToString()); //I want 【A(a,b) >】 but it shows 【A(a,b) and A(a,b) >】
Console.Read();
DaveG
  • 403
  • 1
  • 5
  • 16

1 Answers1

4

The problem is that your pattern starts at the first A it finds, and then matches until the next > it finds - but as you saw - there could be another A in the middle.
This is explained nicely in the second example here: https://stackoverflow.com/a/3075532/7586

The simplest option is to match explicitly what you want instead of .+, for example:

string Pattern = @"A\(\w+,\w+\)\s*>";

Working example

As the comments suggested, negative character classes can also work, for example A[^A]+>, or if that is too limiting, A\([^()]+\)\s*>.

Another option that would work in this case to use RightToLeft matching, like this:

MatchCollection M1 = Regex.Matches(line, Pattern, RegexOptions.RightToLeft);

RightToLeft would start at the > sign, and then the lazy quntifier .+ will behave as you expect - reaching only the first A it finds. This has a symmetrical problem, potentially failing for multiple >s.
Working example

Kobi
  • 125,267
  • 41
  • 244
  • 277