2013-09-25 74 views
1

C#Regex中是否有等效的java.util.regex.Matcher.hitEnd()C#相当于java Matcher.hitEnd()

Javadoc文档boolean hitEnd()

返回true,如果输入的结尾是由搜索引擎由此匹配执行的最后匹配操作的打击。当此方法返回true时,则可能有更多输入会改变上次搜索的结果。

@return true如果输入的结尾在最后一场比赛中被击中,否则为false

+0

难道这不等于'stuff_here $'? –

+0

你对这种方法的理解是什么 –

+1

[同](http://stackoverflow.com/questions/2526756/can-java-util-regex-pattern-do-partial-matches),但在C#中 – Michael

回答

-1

不可以,但也不是很难建立它自己。如果使用Regex.Matches(…)返回MatchCollection最后成功的匹配,需要(用少量Enumerable帮助)

bool hitEnd = match.Success && input.Length == (match.Index + match.Length) 

和可以很容易地扩展方法:

如果使用Regex.Match(…)(即匹配表达式一次),然后:

static bool HitEnd(this MatchCollection matches, string input) { 
    if (matches.Count == 0) { 
    return false; // No matches at all 
    } 
    var lastMatch = matches.Cast<Match>().Last(); 
    return input.Length == (lastMatch.Index + lastMatch.Length) 
} 
+0

不是这样。你错了。 Java'hitEnd',例如:'regex =“[0-9] abc”; input =“1ab”'。不匹配,但hitEnd是真实的。 – Michael

+0

@Michael如果你对这个失败案例感兴趣......如果可能的话(如果这是一个很大的话)更难解决。 – Richard