regex quantifier **

Documentation for regex quantifier ** assembled from the following types:

language documentation Regexes

From Regexes

(Regexes) regex quantifier **

To quantify an atom an arbitrary number of times, use ** quantifier. The quantifier takes a single Int or a Range on the right hand side that specifies the number of times to match. If Range is specified, the end-points specify the minimum and maximum number of times to match.

say 'abcdefg' ~~ /\w ** 4/;      # OUTPUT: «「abcd」␤» 
say 'a'       ~~ /\w **  2..5/;  # OUTPUT: «Nil␤» 
say 'abc'     ~~ /\w **  2..5/;  # OUTPUT: «「abc」␤» 
say 'abcdefg' ~~ /\w **  2..5/;  # OUTPUT: «「abcde」␤» 
say 'abcdefg' ~~ /\w ** 2^..^5/# OUTPUT: «「abcd」␤» 
say 'abcdefg' ~~ /\w ** ^3/;     # OUTPUT: «「ab」␤» 
say 'abcdefg' ~~ /\w ** 1..*/;   # OUTPUT: «「abcdefg」␤» 

Only basic literal syntax for the right hand side of the quantifier is supported, to avoid ambiguities with other regex constructs. If you need to use a more complex expression; for example, a Range made from variables—enclose the Range into curly braces:

my $start = 3;
say 'abcdefg' ~~ /\w ** {$start .. $start+2}/# OUTPUT: «「abcde」␤» 
say 'abcdefg' ~~ /\w ** {π.Int}/;              # OUTPUT: «「abc」␤» 

Negative values are treated like zero:

say 'abcdefg' ~~ /\w ** {-Inf}/;     # OUTPUT: «「」␤» 
say 'abcdefg' ~~ /\w ** {-42}/;      # OUTPUT: «「」␤» 
say 'abcdefg' ~~ /\w ** {-10..-42}/# OUTPUT: «「」␤» 
say 'abcdefg' ~~ /\w ** {-42..-10}/# OUTPUT: «「」␤» 

If then, the resultant value is Inf or NaN or the resultant Range is empty, non-Numeric, contains NaN end-points, or has minimum effective end-point as Inf, the X::Syntax::Regex::QuantifierValue exception will be thrown:

(try say 'abcdefg' ~~ /\w ** {42..10}/  )
    orelse say ($!.^name$!.empty-range);
    # OUTPUT: «(X::Syntax::Regex::QuantifierValue True)␤» 
(try say 'abcdefg' ~~ /\w ** {Inf..Inf}/)
    orelse say ($!.^name$!.inf);
    # OUTPUT: «(X::Syntax::Regex::QuantifierValue True)␤» 
(try say 'abcdefg' ~~ /\w ** {NaN..42}/ )
    orelse say ($!.^name$!.non-numeric-range);
    # OUTPUT: «(X::Syntax::Regex::QuantifierValue True)␤» 
(try say 'abcdefg' ~~ /\w ** {"a".."c"}/)
    orelse say ($!.^name$!.non-numeric-range);
    # OUTPUT: «(X::Syntax::Regex::QuantifierValue True)␤» 
(try say 'abcdefg' ~~ /\w ** {Inf}/)
    orelse say ($!.^name$!.inf);
    # OUTPUT: «(X::Syntax::Regex::QuantifierValue True)␤» 
(try say 'abcdefg' ~~ /\w ** {NaN}/)
    orelse say ($!.^name$!.non-numeric);
    # OUTPUT: «(X::Syntax::Regex::QuantifierValue True)␤»