RegEx太贪心了

时间:2011-07-29 20:50:38

标签: c# regex greedy regex-greedy

我要求验证范围。 输入格式如下:

string example1 = "anydate between 20100101 ~~ 20100101";
string example2 = "anydate between 20100101 and 20100101";
string example3 = "docid between 1 ~~ 2";

我正在使用以下正则表达式:

\b(\w)*(?<operator>Between|contains)\b(?<prefix>.*).*?(?<OP>~~|and)[  ]?\b(?.*)\b

当用户输入"anydate between 20100101 ~~ 20100101 and test1"时,它失败并且捕获到test1

如何让我的正则表达式变得不那么贪婪,而且只有20100101才会被捕获?

1 个答案:

答案 0 :(得分:0)

你可以尝试

anydate\b(?<op>(between|contains))\b(?<first>[0-9]*)\b(?<op2>(~~|and))\b(?<second>[0-9]*)

编辑猜猜需要什么,但是:

(?<func>\w+)\b(?<op>(between|contains))\b(?<first>\w+)\b(?<op2>(~~|and))\b(?<second>\w+)