开发者

Are ^$ and $^ in PHP regex the same?

开发者 https://www.devze.com 2023-03-13 14:41 出处:网络
Why do both of these regexes match successfully? if(preg_match_all(\'/$^/m\',\"\",$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开发者_开发百科";


$ 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.


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.


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号