gpt4 book ai didi

C# 相当于 java Matcher.hitEnd()

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:55 28 4
gpt4 key购买 nike

在 C# Regex 中是否有等同于 java.util.regex.Matcher.hitEnd() 的东西?

boolean hitEnd() 的 Javadoc:

Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher. When this method returns true, then it is possible that more input would have changed the result of the last search.

@return true iff the end of input was hit in the last match; false otherwise

更多引用hitEnd

最佳答案

要知道是否已经到达终点 -

我认为这就像在正则表达式末尾添加 (\z)? 一样简单,
或您认为可以匹配到结尾的正则表达式中的任何地方。

这是一个你可以做的被动检查,它不会干扰任何
其他构造以任何方式。

这是一个 C# 示例用法:

var str =
"Foo $var1 <br/>Yes\n" +
"......... <br/>\n" +
"......... <br/><br/>\n" +
"Foo $var2 <br/>Yes\n" +
"..........<br/>\n" +
"Yes..........<br/>\n" +
"..........<br/>\n" +
"YesYes";

var rx = new Regex(@"Yes(\z)?");

Match M = rx.Match(str);
while (M.Success)
{
bool bAtEnd = M.Groups[1].Success;
Console.WriteLine("Match = {0} , At end {1}", M.ToString(), bAtEnd);
M = M.NextMatch();
}

输出:

Match = Yes , At end  False
Match = Yes , At end False
Match = Yes , At end False
Match = Yes , At end False
Match = Yes , At end True

关于C# 相当于 java Matcher.hitEnd(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19003606/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com