What non-empty string does / ^ $ / match do?

In response to Perl SO, the poster used this code to match blank lines:

$userword =~ /^$/; #start of string, followed immediately by end of string 

Why brian d foy commented:

You cannot say this because it will correspond to one particular non-empty line.

Question What non-empty string matches this? Is this a string consisting only of " \r "?

+8
regex perl
source share
4 answers

Let me check the documents, why not? Reply perlre ,

$ : match the end of the line (or before the new line at the end)

Considering

\z : match only at the end of a line

This means that /^$/ 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 the matching of ^ and $ . In /m , ^ matches at the beginning of any "line", and $ matches before any new line and at the end of the line.

 $ 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 /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 
+7
source share
 use strict; use warnings; use Test::More; ok("\n" =~ /^$/); ok("\n" =~ /^\z/); ok("\n" =~ /^\A\z/); # Better as per brian d. foy suggestion done_testing; 

If you want to check if the string is empty, use /^\z/ or see if length $str is zero (this is what I prefer).

Output:

  ok 1
 not ok 2
 not ok 3 
+9
source share

The regular expression /^$/ matches the non-empty string "\n" .

By default, the Perl regular expression assumes the line contains one β€œline” of text.

^ matches the beginning of a line; in the absence of the /m modifier, this is the same as the beginning of a line.

$ matches either the end of the line or before the new line at the end (which makes /^$/ match the non-empty line "\n" ).

Quote from perldoc perlre :

By default, the β€œ^” character is guaranteed to match only the beginning of a line, the β€œ$” character is only the end (or before newline at the end), and Perl performs certain optimizations with the help that the line contains only one line. Inline newlines will not match "^" or "$". However, you can like a multi-line buffer, so that β€œ^” will match after any newline inside the string (in addition, if the newline is the last character in the string), and β€œ$” will match any newline. For the price of slightly more overhead, you can do this by using the / m modifier on the pattern matching operator.

+3
source share

Script:

 my $str = "\n"; my $test1 = ($str =~ /^$/) ? 1 : 0; my $test2 = ($str =~ /\A\z/) ? 1 : 0; print "$test1, $test2\n"; 

Output:

 1, 0 
+1
source share

All Articles