user6841064
user6841064

Reputation:

Find word before specific phrase in string

I'm trying to find left word located before specific phrase "is better" in all this cases, except input 3:

    string input = "I think that green bike is better than the red bike"; // input 1
    string input = "I think that green bike is better";                   // input 2
    string input = "is better than the red one";                          // input 3
    string input = "bike is better";                                      // input 4

I've tried three ways, but not one of these ways does not give me the desired result, which is to find only left word, in this case it is word "bike" before searching phrase "is better" in all three input cases except input 3 and without searching phrase itself:

1)

    var matches = Regex.Matches(input, @"(?:\S+\s)?\S*is better\S*(?:\s\S+)?", RegexOptions.IgnoreCase);
    var list = matches.Cast<Match>().Select(match => match.Value).ToList();

    foreach (string x in list)
    {
        Console.WriteLine("1) " + x);
    }

2)

     var regex = new Regex(@"(?:is better\s)(?<word>\b\S+\b)");
     var matchCollection = regex.Matches(input);

     foreach (Match match in matchCollection)
     {
        Console.WriteLine("2) " + match.Groups["word"].Value);
     }

3)

string pattern = @"(?<before>\w+) is better (?<after>\w+)";           
MatchCollection matche = Regex.Matches(input, pattern);

for (int i = 0; i < matche.Count; i++)
{
    Console.WriteLine("3) before: " + matche[i].Groups["before"].ToString());
    Console.WriteLine("3) after: " + matche[i].Groups["after"].ToString());
}

With input 1: "I think that green bike is better than the red bike" results is:

1) bike is better than
2) than
3) before: bike
3) after: than

So result of 1) is a both, left and right words of phrase "is better". Result of 2) is "then" word after "is better". And result of 3) is again both words before and after, exactly what I can use, but problem with this solution is shown in second results.

With input 2: "I think that green bike is better" result is:

1) bike is better

Result of 1) is a word "bike" which is before phrase "is better" but with searching phrase "is better". Result of 2) is nothing as it looks for the word after "is better", so it is correct as it is. And result of 3) is also nothing even if word "bike" is exist before "is better" in case if word after "is better" does not exist and it is a last words in string.

With input 3: "is better than the red one" results is:

1) is better than
2) than

Result of 1) is a exist right word after "is better" because left word before does not exist, and again with searching phrase "is better" included. And result 1) is a word "then" after "is better".

And result with input 4: "bike is better":

1) bike is better

Upvotes: 3

Views: 928

Answers (2)

Karthik
Karthik

Reputation: 979

Primitive way (\w+) is better Group 1 : will give you.

Upvotes: 0

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56222

Try this one:

\w+(?=\sis better)

It will match bike except 3rd input.

Upvotes: 1

Related Questions