user1495523
user1495523

Reputation: 505

How do parentheses change the result of a regular expression match?

Could anyone explain to be the difference between the following two syntax:

($x) = $a =~ /(\d+)/;

$y = $a =~ /(\d+)/;

In the example, if $a=100lkj then $x = 100 but $y = 1.

With this code I am trying to extract the numerical value present in $a string.

I don't exactly understand why?

Upvotes: 4

Views: 342

Answers (3)

ysth
ysth

Reputation: 98388

Because regex tests are commonly used in boolean context, the scalar context return is always success or failure, not a captured value, and only list context will give you captured values. If it always returned a captured value, a captured '0' would look like failure in a boolean test.

As far as I am concerned, it is absolutely recommended to use the return value whenever possible; but to do so, you must use list context, such as a list assignment.

Upvotes: 4

user1558455
user1558455

Reputation:

When you write a Variable inside parenthesis, it forces list context. Which means, that the stuff you want to assign to that variable will be interpreted as a list as well.

In your case, you have a normal match. The return value of a match is a list, containing all matches. If you force a list onto scalar context, the number of entries inside this list is returned. So you have 1 match, which means that this list contains 1 match.

Its not very recommended to use the return value of a regular expression.

You could use the variables $1, $2, $3,... for the matches ( matches from parenthesis ).

In your case:

$a =~ /(\d+)/;
$x = $1;

BTW: $a and $b are special Variables for sort. Please dont use them :).

($x) = $a =~ /(\d+)/;
# $x is the first element of the RegEx return value
# ($x, $y, $z) = $a =~ /(\d)(\d)(\d)/;
# $x = first match, $y = second and so on.

Upvotes: 6

Guntram Blohm
Guntram Blohm

Reputation: 9819

In list mode, =~ returns a list of matches. If your $a was abc123def456ghi, the first expression would return (123, 456). You assign the first of these matches to $x.

In scalar mode, the =~ operator returns the number of matches found, which is 1 in your case.

To extract values, don't use the return value of the regex operator, instead, use the $& and $1 .. $9 variables.

Upvotes: 1

Related Questions