• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

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.

  • 11
Reply Report
    • As an interesting aside, I find this scenario similar to the question of why the empty string is a substring of itself. So this is not a stupid question at all. In fact I've just upvoted it!

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.

  • 3
Reply Report

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!

  • 1
Reply Report

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
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions