Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Determing whether a regexp has capturing matches

by adrianh (Chancellor)
on Feb 27, 2007 at 15:35 UTC ( #602310=perlquestion: print w/replies, xml ) Need Help??
adrianh has asked for the wisdom of the Perl Monks concerning the following question:

Is there a nice way I can figure out whether my regexp has capturing matches in it? My question turned up with some code that (boiled down) needs to differentiate between these two cases:

my @x = ( "1234" =~ m/(\d)/ ); # @x contains a captured match of "1" my @y = ( "1234" =~ m/34/ ); # @y contains a "1" for a successful m +atch print "x=(@x), y=(@y)\n"; # x=(1), y=(1)

Now I guess I could set $1 to undef before the match and test it afterwards - but this seems so darn inelegant. Am I missing a trick?

Replies are listed 'Best First'.
Re: Determing whether a regexp has capturing matches
by rhesa (Vicar) on Feb 27, 2007 at 15:52 UTC
      D'oh. Of course. Thanks.
      sub has_captured_matches { return $#+ };

        Keep in mind that $#+ is -1 when the pattern does not match, regardless of whether there are captures.

        "1234" =~ /(1)34/; print $#+, "\n"; "1234" =~ /134/; print $#+, "\n";

        That prints "-1" both times, and -1 is considered a "true" value.

Re: Determing whether a regexp has capturing matches
by kyle (Abbot) on Feb 27, 2007 at 15:45 UTC

    You don't need to set $1 before the match. In fact, I think you can't.

    $ perl -le '$1=undef;' Modification of a read-only value attempted at -e line 1.

    $1 will be set to undef after a match with no parentheses.

    "1234"=~/(\d)/; print "$1\n"; "1234"=~/34/; print "$1\n"; print "undef\n" if ! defined $1;


    1 undef

    This doesn't work when a regex had capturing parentheses that didn't match. Just like the "no parentheses" case above, this also prints "undef":

    "1234"=~/(x)?34/; print "undef" if ! defined $1;
      $1 will be set to undef after a match with no parentheses.

      Is this documented behaviour? I could have sworn I'd used a perl at some point where $1 persisted unless you made another captured match. I could, of course, be making shit up :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://602310]
Approved by Old_Gray_Bear
[hippo]: I had a low-paid job about 20 years ago and seriously considered going down to a 3-day week. Would have worked 60% of the time for about 80% of the cash.
[Corion]: ambrus: Well, I pay rent, but don't own a house with variable/ unforeseeable costs
[hippo]: The thesholds/benefits balance at the time was nuts.
[hippo]: But that doesn't last because, you know ... politicians. :(
[Corion]: hippo: Yeah, if you still get enough money to get by and don't have expensive hobbies or other fixed costs that can work out well
[ambrus]: ah good. I pay only rent too. but that still costs significant money.
[Corion]: ambrus: Yes, but that is already budgeted for

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-09-21 15:19 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (249 votes). Check out past polls.