Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

perl

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?

like image 725
DVK Avatar asked Apr 18 '12 17:04

DVK


2 Answers

use strict;
use warnings;

use Test::More;

ok("\n" =~ /^$/);
ok("\n" =~ /^\z/);
ok("\n" =~ /^\A\z/); # Better as per brian d. foy's suggestion

done_testing;

If you want to test if a string is empty, use /^\z/ or see if length of $str is zero (which is what I prefer).

Output:

ok 1
not ok 2
not ok 3
like image 113
Sinan Ünür Avatar answered Sep 29 '22 05:09

Sinan Ünür


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
like image 26
ikegami Avatar answered Sep 29 '22 03:09

ikegami