Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

variable quantifiers?

by FryingFinn (Sexton)
on Jan 06, 2014 at 21:37 UTC ( #1069583=perlquestion: print w/replies, xml ) Need Help??
FryingFinn has asked for the wisdom of the Perl Monks concerning the following question:

Can one (or more to the point, I) use a variable as a quantifier value in a regex ? As illustrated in below...

#!/usr/bin/perl -w use constant minUC => 3; my $min = 3; my $test = "AAAAAA"; my @chars = (split //, $test); my $Rep = 0; foreach my $c ( @chars) { $Rep++ if ($test =~ /$c{3,}?/) ; # this works # $Rep++ if ($test =~ /$c{$min,}?/) ; # no work; # $Rep++ if ($test =~ /$c{minUC,}?/) ; #nor does using a named cons +tant }

Replies are listed 'Best First'.
Re: variable quantifiers?
by AnomalousMonk (Canon) on Jan 06, 2014 at 21:57 UTC
    >perl -wMstrict -le "my $c = 'A'; my $min = 3; ;; my $s = 'uuAvvAAwwAAAxxAAAAyy'; ;; printf qq{'$1' } while $s =~ m{ ([[:lower:]] (?:$c){$min,} [[:lower:] +]) }xmsg; " 'wAAAx' 'xAAAAy'

    Update: The quantified sub-pattern in  (?:$c){$min,} needs to be enclosed in a group (non-capturing or capturing) because otherwise  $c{$min,} looks to Perl too much like the interpolation of a scalar hash element from the  %c hash.

    Further Update: Interesting. I was unaware of the  \ (backslash) disambiguation pointed out below.

    Further Further Update: Or maybe not so interesting, since the regex doesn't actually seem to match anything:

    >perl -wMstrict -le "my $c = 'A'; my $min = 3; $_ = 'AAAAAA'; print 'match' if /($c\{$min,})/; print qq{'$1'}; " Use of uninitialized value $1 in concatenation (.) or string at -e lin +e 1. ''

    Yet Another Update: It is also possible to use a constant, but it's kinda messy. A Readonly scalar might do a better job for you.

    >perl -wMstrict -le "use constant MIN => 3; my $c = 'A'; ;; my $s = 'uuAvvAAwwAAAxxAAAAyy'; ;; printf qq{'$1' } while $s =~ m{ ([[:lower:]] (?:$c){${ \MIN },} [[:lower:]]) }xmsg; " 'wAAAx' 'xAAAAy'
      $ perl -e " use re 'debug'; $c='a'; $min=3; m/$c\{$min,}/; " Compiling REx "a\{3,}" Final program: 1: EXACT <a{3,}> (4) 4: END (0) anchored "a{3,}" at 0 (checking anchored isall) minlen 5 Freeing REx: "a\{3,}"
Re: variable quantifiers? (TIMTOWTDI)
by LanX (Chancellor) on Jan 07, 2014 at 01:23 UTC
    space separation under /x flag seems to do the job
    DB<113> $_="AAAA"; $c="A"; $min=3; /$c {$min}/x; $& => "AAA"
    UPDATE
    TIMTOWTDI
    DB<108> $c="A";$min=3 => 3 DB<109> "AAAAA" =~ m/${c}{$min}/; $& => "AAA"
Re: variable quantifiers?
by lidden (Curate) on Jan 07, 2014 at 06:32 UTC
    There is always eval STRING if nothing else works.
      The problem is the ambiguity of '$c{$min}' like AnomalousMonk already explained.

      eval also only operates on strings. So it won't allow to solve this variable interpretation problem differently, it'll only add more complexity.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      Not in this case... Try and see!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069583]
Approved by Corion
Front-paged by toolic
help
Chatterbox?
[stevieb]: ++ atcroft
[atcroft]: .oO(Then there is the effect if a site changes their timezone, such as when the International Date Line was moved by the purchase of Alaska by the US from Russia in 1867, or several places (I cannot recall off-hand) that moved from one side of the Date

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-04-29 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (531 votes). Check out past polls.