怎么看怎么正则表达式的远指数继续落选赛怎么看、指数、继续、正则表达式

2023-09-03 07:57:09 作者:人間禍害

可能重复:   Regex指数匹配的字符串,其中比赛失败

如果我有这个问题:

0123456789abcdef...

和我有图案

\d+TEST

显然会失败。现在,我想知道,在达到最大指数。换句话说,正则表达式引擎会做的:

clearly it will fail. Now I would like to know the maximum index that was reached. In other words the regex engine would have done:

首先它匹配数字所以它移动了它的索引到下一个字符,这也是一个数字。这个步骤将重复,直到正则表达式引擎搜索次数 9 。因为下一个字符不是 T 失败。 我会想获得指数 10 ,因为正则表达式引擎成功地移动10个字符,直到它失败了。有没有得到这个信息的一种方式?我要查的几个文件拷贝正确的,我做它与一个正则表达式。这将是很好,如果我能知道什么指数的正则表达式失败。

First it matches a digit so it moved its index to the next character which is also a digit. This steps will be repeated until the regex engine finds the number 9. Because the next character is not a T it fails. I will like to get the index 10 because the regex engine successfully moved 10 characters until it failed. Is there a way of getting this info? I have to check the copy right of several documents and I do it with a regex. It will be nice if I could know on what index the regex failed.

推荐答案

您需要做两件独立的搜索。

You need to do two separate searches.

在您的例子code,正则表达式引擎实际上已经达到了字符串的结尾 - 拒绝的字符串为没有比赛时,发动机本身考虑每一个可能的匹配。你不能得到的信息,我搬到10号人物,并停止,因为正则表达式引擎实际移动到第16个字符之前,它决定性地结束了。

In your example code, the regex engine has in fact reached the end of the string - the engine itself considers every possible match when rejecting the string as having no matches. You can't get the information "I moved to the 10th character and stopped" because the regex engine actually moved to the 16th character before it finished conclusively.

有关你问什么,你首先会做到这一点:

For what you're asking, you would first do this:

string source = @"0123456789abcdef";
Regex r = new Regex(@"\d+TEST")
MatchCollection matches = r.Matches(source); // Returns no matches

这将返回是否有匹配的完整的字符串存在。如果失败,然后执行此:

This would return whether or not a match for your full string exists. If it fails, then execute this:

if (matches.Count == 0) {
    r = new Regex(@"\d+");
    MatchCollection matches = r.Matches(source);
    int maxpos = -1;
    foreach (Match m in matches) {
        if (m.Index + m.Length > maxpos) maxpos = m.Index + m.Length;
    }

    // returns 10
    return maxpos;
}

编辑:另一种选择是使TEST的字符串一个可选的匹配。然后,您可以查看比赛的名单,其中包括两场比赛只在数字和相匹配的数字+测试字符串。

One other alternative is to make the "TEST" string an optional match. You can then review the list of matches, which will include both matches only on the digits and matches on the digits + TEST string.

string source = @"0123456789abcdef";
Regex r = new Regex(@"\d+(TEST)?")
MatchCollection matches = r.Matches(source); // Returns one match of 10 digits at position 0 - 10.