使用零元素/忽略最后一个char时匹配集合的问题 [英] problem with match collection in using zero element/ignoring last char

查看:74
本文介绍了使用零元素/忽略最后一个char时匹配集合的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很少的用户输入,我试图通过MatchCollection与我有不同的模式进行比较,即



  string  user =   \ b(?:I | would | like |要|见| ID |的|买|好)\b; 







但当我匹配字符串



  string  pattern =   ^?。(= * \bgoods?|?项目?|?事\b)(?= * \bbought\b)(= * \bid\b )(= * \btest\b)* $?。; 

MatchCollection mat = Regex.Matches(pattern,user);

foreach var item in mat)
{
Console.WriteLine(item.ToString());
}





匹配上述两项时,匹配集合只会产生/收集匹配(id,但是忽略了第一块模式中的商品。它似乎无视**?**前瞻提到商品?|物品?|东西?



任何建议。





谢谢

解决方案

;

MatchCollection mat = Regex.Matches(pattern,user);

foreach var item in mat)
{
Console.WriteLine(item.ToString());
}





匹配两者上面,匹配集合只产生/收集(id,buy)的匹配但忽略了第一块模式中的货物。它似乎忽略了**?**前瞻提到货物?|物品?|东西?。



任何建议。





谢谢


您好,



将您的表情更改为:



  string  pattern =  (?=。* \\ \\bgoods \b | \bitems\b |?。?。?。\bthings \b)(= * \bbought\b)(= * \bid\b)(= * \ btest \b)。*; 





投入? '''之后''中的东西?'会捕捉到东西和东西。



你需要在可选列表中的每个单词周围加上单词边界。

(您在用户输入中也拼错了货物,但我认为您的意思是货物。 )


I have few user inputs which I am trying to compare via MatchCollection with different patterns I have i.e

string user = "\b(?:I|would|like|to|see|id|of|bought|good)\b";




but When I am matching string

string pattern = "^(?=.*\bgoods?|items?|things?\b)(?=.*\bbought\b)(?=.*\bid\b)(?=.*\btest\b).*$";

MatchCollection mat = Regex.Matches(pattern , user );

foreach (var item in mat)
{
    Console.WriteLine(item.ToString());
}



When matching the two above, the match collection only yields/collects match for (id, bought) but ignores "goods" from the first block of pattern. It seems to be ignoring **?** lookahead mentioned goods?|items?|things?.

Any suggestions.


Thanks

解决方案

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



When matching the two above, the match collection only yields/collects match for (id, bought) but ignores "goods" from the first block of pattern. It seems to be ignoring **?** lookahead mentioned goods?|items?|things?.

Any suggestions.


Thanks


Hi,

Change your expression to this:

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



Putting a ? after the 's' in 'things?' will catch both thing and things.

You need to have word boundaries around each word in the optional list.
(You also misspelled goods as good in your user input, but I assume you mean goods.)


这篇关于使用零元素/忽略最后一个char时匹配集合的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆