简体   繁体   English

跳过比赛集合正则表达式中单词的最后一个字符

[英]Skip last character in words in match collection Regex

I am matching few words strings with below expression. 我用下面的表达式匹配几个单词字符串。

    string pattern = "(?=.*\bgoods\b|\bitems\b|\bthings\b).*$;

    string matchWord1 = goods;
    string matchWord2 = items;
    string matchWord3 = things;

Could you also please guide if below expression can be amended to skip last character at the end of every word in boundary? 您能否也请指导一下是否可以修改下面的表达式以跳过边界中每个单词末尾的最后一个字符? .ie .ie

Desire matchWords with the same string pattern: 希望使用具有相同字符串模式的matchWords:

    string matchWord1 = good;
    string matchWord2 = item;
    string matchWord3 = thing;

So that Regex ignore the plural s at the end and still match above plural words. 这样Regex会在末尾忽略复数s ,但仍在复数词上方匹配。

I have tried using ? 我尝试使用 with the following but its not working. 与以下,但它无法正常工作。

    string pattern = "^(?=.*\bgoods?\b|\bitems?\b|\bthings?\b).*$;

or 要么

    string pattern = "^(?=.*\b(?:good|item|thing)s?\b).*$";



    string input = @"\b(?:I|would|like|to|see|id|of|bought|things|items)\b";

    MatchCollection mat = Regex.Matches(pattern, input, RegexOptions.IgnoreCase);
    foreach (var item in mat)
    {
      Console.WriteLine(item.ToString());
    }

Thanks in advance. 提前致谢。

If you want to validate with a regular expression match if the entire string is either good or item or thing in singular or plural with getting returned the singular word only on match, use 如果您想用正则表达式匹配来验证整个字符串是良品还是单数或复数的项目事物 ,并且仅在匹配时返回单数字,请使用

string pattern = "^(good|item|thing)s?$";

But if the matching word should be returned in singular or plural as found, use 但是,如果匹配的单词应按发现的单数或复数形式返回,请使用

string pattern = "^((?:good|item|thing)s?)$";

In case of wanting to find anywhere within a string at least one of the 3 words in singular or plural with getting returned the singular word only on match, use 如果想字符串中的任何地方找到至少3个单数或复数单词之一,并且仅在匹配时才返回单数单词,请使用

string pattern = "\\b(good|item|thing)s?\\b";

And again if the found word should be returned in singular or plural as found, use 再一次,如果找到的单词应该以找到的单数或复数形式返回,请使用

string pattern = "\\b((?:good|item|thing)s?)\\b";

The main mistake you have made resulting in not working regular expressions is that \\b in a Perl regular expression means word boundary . 您导致导致不起作用的正则表达式的主要错误是,Perl正则表达式中的\\b表示单词边界

But the escape character in C# regular-string-literals is the backslash character and therefore the compiler stores in the EXE for \\b in source code just character b . 但是C#常规字符串文字中的转义字符是反斜杠字符,因此编译器将源代码中\\b中的\\b仅存储在字符b Now it is clear why non of your regular expression matched something. 现在很清楚为什么您的正则表达式都不匹配某些东西。

By escaping the backslash for the regular expression with another backslash and therefore use in source code \\\\b , the compiler really writes into EXE the string \\b and regular expression works. 通过将正则表达式的反斜杠转义为另一个反斜杠,并因此在源代码\\\\b ,编译器实际上会将字符串\\b写入EXE,并且正则表达式可以正常工作。

Keep that in mind for the future: each backslash in a regular expression string in C# source code must be escaped with an additional backslash, except you use a verbatim-string-literal definition which is better for regular expressions. 将来请记住:C#源代码中正则表达式字符串中的每个反斜杠都必须使用附加的反斜杠进行转义,除非您使用逐字字符串字面量定义,该定义更适合于正则表达式。

Example for a verbatim-string-literal definition of last regular expression string from above: 上面最后一个正则表达式字符串的逐字逐字逐字定义的示例:

string pattern = @"\b((?:good|item|thing)s?)\b";

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

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