Spiderman5
Spiderman5

Reputation: 23

Regular expressions, find matches

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

Answers (1)

anubhava
anubhava

Reputation: 785156

You can use lookahead regex:

\b[Ss]ome\b(?=.*hello)

RegEx Demo

This will match some or Some only if it is followed by a hello.

Upvotes: 1

Related Questions