2013-09-25 50 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