Reputation: 4402
I'm learning perl and regex. The following code:
my $data = "Bule beatles battling a blowing breeze";
my $results = $data =~ m/beatles battling/;
print "results: $results\n";
produces this output:
C:\scripts\perl\sandbox>regex.pl
results: 1
With a small change, I get the results I want:
print "results: $&\n";
results: beatles battling
I also noticed that I don't need to create $results:
my $data = "Bule beatles battling a blowing breeze";
$data =~ m/beatles battling/;
print "results: $&\n";
In the various perl tutorials I've been reading, some examples use the following syntax:
my $results = $data =~ m/string_to_match/;
My questions:
For example:
my $data = "Bule beatles battling a blowing breeze";
my $results1 = $data =~ m/beatles/;
my $results2 = $data =~ m/battling/;
Many thanks :)
Upvotes: 2
Views: 6025
Reputation: 71558
When should one create $results instead of directly doing a regex on $data?
Sometimes, you would want to know whether there is a match or not, and this (also answering your second question) is what the 1
is. Therefore, if there is a match, you get 1
, and 0
if you don't get any matches.
Why do I get a "1" returned when I try to print $data? (I think it's returning length of matches...)
I believe I answered this above :)
Is it useful to create $results? If no, how would I deal with multiple results?
I personally have rarely stored the value 1
or 0
in a variable. One possible scenario I guess could be when you are using the regex in a function which contains a series of validations and then comparing this result with the result of the other validations to conclude whether the function will return true or not (or valid or not).
I have found myself more using match regex in if()
. Maybe something like...
if ($data =~ m/beatles/)
{
# Do something
} else {
# Do something else
}
For your example:
my $data = "Bule beatles battling a blowing breeze";
my $results1 = $data =~ m/beatles/;
my $results2 = $data =~ m/battling/;
You could compare $results1
and $results2
to conclude whether the string $data
contains both the words beatles
and battling
, anywhere in the string.
You can have a read through this page describing the different special variables in perl and you'll find there that $&
contains the string matched by the last pattern match. In your example, after running both regexes, you'll get $& = "battling"
and not beatles
.
Upvotes: 4
Reputation: 57640
A normal regex match in scalar context returns a boolean value that indicates if the pattern matched. It does not return the matched substring (inefficient!).
To access the matched substring, enclose the pattern in parens. The contents of that pattern are then available in $1
or as the first return value in list context:
my $data = "Bule beatles battling a blowing breeze";
my ($result) = $data =~ /(beatles battling)/;
say $result;
Output: beatles battling
.
If you have more captures in your pattern, their contents will be in $2
, $3
, …. You can also access them via list context:
my ($substring, $beatles, $battling) = $data =~ /((beatles) (battling))/;
Never use $&
, except maybe when golfing, or on a one-liner where efficiency or good style is not an issue.
Usage of $&
etc. imposes an overhead on all pattern matches globally. You don't want that.
Upvotes: 9