Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Calculating variable {n} in regex match

by temporal (Pilgrim)
on Jun 05, 2012 at 17:08 UTC ( #974507=note: print w/replies, xml ) Need Help??


in reply to Calculating variable {n} in regex match

It sounds like the OP wants to capture the number of occurrences entirely within the regex, something like:

$foo =~ m/[0-9]({\d+})/ print "$foo has $1 digit(s)";

... if that were possible (it isn't). This would allow the counting match to be thrown in with a longer regex that is matching on other properties as well. This is probably possible through clever use of code evaluation (?{code}) and interpolation within the regex.

However, if all you need to do is count in the regex then you could use the 'goatse' operator:

my $length =()= $foo =~ /[0-9]/g; print "$foo has $length digit(s)";

Seems better than looping and incrementing, anyway.

Strange things are afoot at the Circle-K.

Replies are listed 'Best First'.
Re^2: Calculating variable {n} in regex match
by AnomalousMonk (Canon) on Jun 05, 2012 at 19:49 UTC

    FWIW:

    >perl -wMstrict -le "my $s = '123 234 3456789 3210 34567'; ;; my @m = $s =~ m{ \b ((\d) (??{ qr{\d{$^N}} })) \b }xmsg; print qq{@m}; ;; my $i = 2; @m = $s =~ m{ \b (??{ ++$i; qr{\d{$i}}; }) \b }xmsg; print qq{@m}; " 234 2 3210 3 123 3456789
      okay, AnomalousMonk, this works:
      # the original number of integers to test (will be one-upped) my $i = 1; # the input string to be tested (a number) my $foo = shift; # see if the input string is a two-digit number my $rc = ($foo =~ m{ \b (??{ ++$i; qr{\d{$i}}; }) \b }xmsg) ?'Y' :'N';
      very clever...thanks.

        But sometimes cleverness can be a curse, so you may want to reserve your thanks until later.

Re^2: Calculating variable {n} in regex match
by atreyu (Sexton) on Jun 05, 2012 at 18:03 UTC
    ... if that were possible (it isn't). This would allow the counting match to be thrown in with a longer regex that is matching on other properties as well. This is probably possible through clever use of code evaluation (?{code}) and interpolation within the regex.

    that's actually kind of hoping for...a clever use of eval - but i couldn't figure that out. anyway, sounds like what i want is not possible, at least in a straight-forward fashion.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://974507]
help
Chatterbox?
LanX LOL of the day ... the Turkish Anonymous Alcoholics stopped their "No to alcohol" campaign for political reasons ...
LanX omg my browser is posting again on its own ...
[LanX]: choroba: GREAT JOKE! xD
LanX starts googling the punch line ...
[choroba]: suddenly, a weird noise can be heard, and the conductor stops them
[choroba]: (sorry, was interrupted by a call)
[choroba]: What was that? asks he
[choroba]: and the triangle player says, "That was me. It's written here in the score"

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-03-27 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (319 votes). Check out past polls.