gpt4 book ai didi

匹配精确字符串的正则表达式(不允许终止换行符)

转载 作者:行者123 更新时间:2023-12-04 01:58:43 25 4
gpt4 key购买 nike

在 .NET System.Text.RegularExpressions.Regex 中,如果将 ^$ 添加到 Regex 中以查找精确匹配,它如果将终止 \n 附加到正在验证的字符串,IsMatch 仍会返回 true。

例如下面的代码:

Regex regexExact = new Regex(@"^abc$");
Console.WriteLine(regexExact.IsMatch("abc"));
Console.WriteLine(regexExact.IsMatch("abcdefg"));
Console.WriteLine(regexExact.IsMatch("abc\n"));
Console.WriteLine(regexExact.IsMatch("abc\n\n"));

返回:

true
false
true
false

什么是正则表达式,除了第一个,上面所有的都返回 false?

最佳答案

当前 .NET 正则表达式的解决方案

您应该在 .NET 正则表达式中使用字符串 anchor 的末尾 \z:

Regex regexExact = new Regex(@"^abc\z");

参见 Anchors in Regular Expressions :

$    The match must occur at the end of the string or line, or before \n at the end of the string or line. For more information, see End of String or Line.
\Z    The match must occur at the end of the string, or before \n at the end of the string. For more information, see End of String or Before Ending Newline.
\z    The match must occur at the end of the string only. For more information, see End of String Only.

可以在 中使用相同的 anchor , , , , 。在 , 使用 \Z。在 JavaScript RegExp (ECMAScript) 兼容模式中,$ anchor 匹配字符串的末尾(如果没有 /m 修饰符已定义)。

背景

参见 Strings Ending with a Line Break在 regular-expressions.info:

Because Perl returns a string with a newline at the end when reading a line from a file, Perl's regex engine matches $ at the position before the line break at the end of the string even when multi-line mode is turned off. Perl also matches $ at the very end of the string, regardless of whether that character is a line break. So ^\d+$ matches 123 whether the subject string is 123 or 123\n.

Most modern regex flavors have copied this behavior. That includes .NET, Java, PCRE, Delphi, PHP, and Python. This behavior is independent of any settings such as "multi-line mode".

In all these flavors except Python, \Z also matches before the final line break. If you only want a match at the absolute very end of the string, use \z (lower case z instead of upper case Z). \A\d+\z does not match 123\n. \z matches after the line break, which is not matched by the shorthand character class.

In Python, \Z matches only at the very end of the string. Python does not support \z.

关于匹配精确字符串的正则表达式(不允许终止换行符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48831983/

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