Which non-empty string does /^$/ match?

perl, regex

Solution

Let's check the docs, why don't we? Quote perlre,

`$`: Match the end of the line (or before newline at the end)

Given

`\z`: Match only at end of string

That means `/^$/` is equivalent to `/^\n?\z/`.

$ perl -E'$_ = "";    say /^$/ ||0, /^\n?\z/ ||0, /^\z/ ||0;'
111

$ perl -E'$_ = "\n";  say /^$/ ||0, /^\n?\z/ ||0, /^\z/ ||0;'
110

Note that `/m` changes what `^` and `$` match. Under `/m`, `^` matches at the start of any "line", and `$` matches before any newline and at the end of the string.

$ perl -E'$_ = "abc\ndef\n";  say "matched at $-[0]" while  /^/g'
matched at 0

$ perl -E'$_ = "abc\ndef\n";  say "matched at $-[0]" while  /$/g'
matched at 7
matched at 8

And using /m:

$ perl -E'$_ = "abc\ndef\n";  say "matched at $-[0]" while  /^/mg'
matched at 0
matched at 4   <-- new

$ perl -E'$_ = "abc\ndef\n";  say "matched at $-[0]" while  /$/mg'
matched at 3   <-- new
matched at 7
matched at 8

`\A`, `\Z` and `\z` aren' t affected by `/m`:

$ perl -E'$_ = "abc\ndef\n";  say "matched at $-[0]" while  /\A/g'
matched at 0

$ perl -E'$_ = "abc\ndef\n";  say "matched at $-[0]" while  /\z/g'
matched at 8

$ perl -E'$_ = "abc\ndef\n";  say "matched at $-[0]" while  /\Z/g'
matched at 7
matched at 8

Problem

In a Perl SO answer, a poster used this code to match empty strings: ``` $userword =~ /^$/; #start of string, followed immediately by end of string ``` To which brian d foy commented: You can't really say that because that will match one particular non-empty string. Question: Which non-empty string is matched by this? Is it a string consisting of "`\r`" only?

Original source

Related problems