PHP 正则表达式中的 ^$ 和 $^ 相同吗?

发布于 2024-11-16 03:14:42 字数 164 浏览 4 评论 0原文

为什么这两个正则表达式都匹配成功?

if(preg_match_all('/$^/m',"",$array))
  echo "Match";

if(preg_match_all('/$^\n$/m',"\n",$array))
  echo "Match";

Why do both of these regexes match successfully?

if(preg_match_all('/$^/m',"",$array))
  echo "Match";

if(preg_match_all('/$^\n$/m',"\n",$array))
  echo "Match";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你曾走过我的故事 2024-11-23 03:14:42

$^ 是零宽度元字符。与每次匹配一个字符(除非与量词一起使用)的其他元字符(如 .)不同,它们实际上并不匹配文字字符。这就是 ^$ 匹配空字符串 "" 的原因,即使正则表达式(无分隔符)包含两个字符,而空字符串包含零。

空字符串不包含任何字符并不重要。它仍然有一个起点和一个终点,并且由于它是一个空字符串,因此两者位于同一位置。因此,无论您使用 ^$ 的顺序或数量,它们的所有排列都应与空字符串匹配。


您的第二种情况稍微棘手一些,但适用相同的原则。

m 修饰符 (PCRE_MULTILINE) 只是告诉 PCRE 引擎一次性输入整个字符串,而不考虑换行符,但字符串仍然包含“多行”。然后,它分别将 ^$ 视为“行的开头”和“行的结尾”。

字符串 "\n" 本质上在逻辑上分为三部分:"""\n""" (因为换行符被空虚包围......听起来很诗意)。

然后这些匹配如下:

  1. 第一个空字符串与起始 $^ 匹配(正如我上面所解释的)。

  2. \n 与正则表达式中的相同 \n 匹配。

  3. 第二个空字符串与最后一个 $ 匹配。

这就是第二种情况导致匹配的方式。

$ and ^ are zero-width meta-characters. Unlike other meta-characters like . which match one character at a time (unless used with quantifiers), they do not actually match literal characters. This is why ^$ matches an empty string "", even though the regex (sans delimiters) contains two characters while the empty string contains zero.

It doesn't matter that an empty string contains no characters. It still has a starting point and an ending point, and since it's an empty string both are at the same location. Therefore no matter the order or number of ^ and $ you use, all of their permutations should match the empty string.


Your second case is slightly trickier but the same principles apply.

The m modifier (PCRE_MULTILINE) just tells the PCRE engine to feed in the entire string at one go, regardless of newlines, but the string still comprises "multiple lines". It then looks at ^ and $ as "the start of a line" and "the end of a line" respectively.

The string "\n" is essentially logically split into three parts: "", "\n" and "" (because the newline is surrounded by emptiness... sounds poetic).

Then these matches follow:

  1. The first empty string is matched by the starting $^ (as I explain above).

  2. The \n is matched by the same \n in your regex.

  3. The second empty string is matched by the last $.

And that's how your second case results in a match.

我纯我任性 2024-11-23 03:14:42

不,不是。实际上,表达式 $^ 永远不应该匹配,因为 $ 表示字符串的结尾,而 ^ 表示字符串的开头。但正如我们所知,结尾不能出现在字符串开头之前:)

^$ 应该匹配空字符串,仅此而已。

“行首”元字符 (^) 仅在字符串的开头匹配,而“行尾”元字符 ($) 仅在字符串的末尾匹配,[...]

来自 PCRE 手册页

请注意,通过添加 PCRE_MULTILINE 修饰符,$ 成为 EOL 并且^ 变为 BOL,它将匹配(感谢 netcoder 指出这一点)。不过,我个人不会使用它。

No it is not. Actually, the expression $^ should never match, because $ symbolizes the end of a string whereas ^ represents the beginning. But as we know, the end cannot come before the beginning of a string :)

^$ should match an empty string, and only that.

The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, [...]

From the PCRE manpages

Note that, by adding the PCRE_MULTILINE modifier, $ becomes EOL and ^ becomes BOL, it will match (thanks netcoder for pointing that out). Still, I personally wouldn't use it.

十年不长 2024-11-23 03:14:42

Regex.IsMatch ("", "$^") 在 C# 中也匹配。由于它是一个空字符串,因此没有大小。在索引 -1 处,它同时位于字符串的末尾和开头。好问题!

Regex.IsMatch ("", "$^") matches in C#, also. Since it is an empty string, there is no size. At index -1, it is both at the end and beginning of the string, simultaneously. Good question!

挽容 2024-11-23 03:14:42

在正则表达式中,^ 匹配字符串的开头,$ 匹配字符串的结尾。

因此,正则表达式 /^$/ 将成功匹配完全空的字符串(没有其他内容)。

/$^/ 不会匹配任何内容,因为从逻辑上讲,字符串的结尾不能在它的开头之前。

In regex, ^ matches the start of the string, and $ matches the end of the string.

Therefore, regex /^$/ will successfully match a completely empty string (and nothing else).

/$^/ will not match anything, as logically you can't have the end of the string before the beginning of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文