【发布时间】:2013-03-09 01:34:38
【问题描述】:
我有这个字符串:
of cabbages and kings and more kings
我想根据这个条件进行匹配:
- Any instance of "king"
- Any instances of "cab" OR "goat"
- No instances of "bill"
此正则表达式适用于 IsMatch():
^(?=.*?(king))(?=.*?(cab|goat))(?=.*?(bill))
但它只返回 1 个长度为 0 的组。有没有办法让它返回匹配的组,以便我们可以在之后读取匹配的文本?
【问题讨论】:
-
它不起作用(除非你的规范是错误的),什么匹配的文本?你的意思是整个字符串?
-
你能发布输入和预期输出吗?
-
输入文本是“of cabbages and kings and more kings”,我希望从 Regex.GetMatches 中得到一个包含“king”和“cab”的 MatchCollection。
-
“没有 'bill' 的实例”必须是否定前瞻(例如
(?!.*?(bill))。