Reputation: 23
This regular expression gives answer 3 for count. How can I take only first and second "some" before "hello" ? Help me, please.
string SomeText ="Some some hello some"
string patternSome = @"some";
RegexOptions RegOptions = RegexOptions.IgnoreCase |RegexOptions.CultureInvariant;
Regex newRegex = new Regex(patternSome, RegOptions );
MatchCollection matches = newRegex.Matches(SomeText);
Console.WriteLine("Count of matches {0}", matches.Count);
Upvotes: 1
Views: 28
Reputation: 785156
You can use lookahead regex:
\b[Ss]ome\b(?=.*hello)
This will match some
or Some
only if it is followed by a hello
.
Upvotes: 1