简体   繁体   English

在字符串中的特定短语之前查找单词

[英]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 : 在所有这些情况下,我试图找到位于特定短语"is better"之前的左词,除了输入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: 我尝试了三种方法,但是这些方法中没有一种方法不能给我想要的结果,也就是只找到左边的单词,在这种情况下,在所有三种输入情况下搜索短语"is better"之前它是单词"bike"输入3和没有搜索短语本身:

1) 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) 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) 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: "I think that green bike is better than the red bike"结果是:

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" . 因此1)结果是短语"is better"左右两个词。 Result of 2) is "then" word after "is better" . 结果2)"then" "is better"后的"is better" "then"字。 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. 3)结果再次是前后两个词,正是我可以使用的,但是这个解决方案的问题在第二个结果中显示。

With input 2: "I think that green bike is better" result is: 输入2: "I think that green bike is better"结果是:

1) bike is better

Result of 1) is a word "bike" which is before phrase "is better" but with searching phrase "is better" . 结果1)是一个单词"bike" ,它在短语"is better"但搜索短语"is better" Result of 2) is nothing as it looks for the word after "is better" , so it is correct as it is. 2)结果没什么,因为它在"is better"之后查找单词,所以它是正确的。 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. 而结果3)也没什么即使词"bike"是之前就存在"is better"的情况下,如果单词后"is better"并不存在,它是在字符串的最后一句话。

With input 3: "is better than the red one" results is: 输入3: "is better than the red one"结果是:

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. 结果1)"is better"后存在的右词,因为之前的左词不存在,并且再次包含搜索短语"is better" And result 1) is a word "then" after "is better" . 结果1)"is better"之后"is better" "then" "is better"

And result with input 4: "bike is better" : 结果输入4: "bike is better"

1) bike is better

Try this one: 试试这个:

\w+(?=\sis better)

It will match bike except 3rd input. 它将匹配除第3输入之外的bike

原始方式(\\w+) is better第1组:会给你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM