In Regexes§
See primary documentation in context for General quantifier: ** min..max
To quantify an atom an arbitrary number of times, use the **
quantifier, which takes a single Int or a Range on the right-hand side that specifies the number of times to match. If a 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 in curly braces:
my = 3;say 'abcdefg' ~~ /\w ** /; # OUTPUT: «「abcde」»say 'abcdefg' ~~ /\w ** /; # OUTPUT: «「abc」»
Negative values are treated like zero:
say 'abcdefg' ~~ /\w ** /; # OUTPUT: «「」»say 'abcdefg' ~~ /\w ** /; # OUTPUT: «「」»say 'abcdefg' ~~ /\w ** /; # OUTPUT: «「」»say 'abcdefg' ~~ /\w ** /; # 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 ** / )orelse say ($!.^name, $!.empty-range);# OUTPUT: «(X::Syntax::Regex::QuantifierValue True)»(try say 'abcdefg' ~~ /\w ** /)orelse say ($!.^name, $!.inf);# OUTPUT: «(X::Syntax::Regex::QuantifierValue True)»(try say 'abcdefg' ~~ /\w ** / )orelse say ($!.^name, $!.non-numeric-range);# OUTPUT: «(X::Syntax::Regex::QuantifierValue True)»(try say 'abcdefg' ~~ /\w ** /)orelse say ($!.^name, $!.non-numeric-range);# OUTPUT: «(X::Syntax::Regex::QuantifierValue True)»(try say 'abcdefg' ~~ /\w ** /)orelse say ($!.^name, $!.inf);# OUTPUT: «(X::Syntax::Regex::QuantifierValue True)»(try say 'abcdefg' ~~ /\w ** /)orelse say ($!.^name, $!.non-numeric);# OUTPUT: «(X::Syntax::Regex::QuantifierValue True)»