is Method
A regex is a kind of pattern that describes a set of strings. The process of finding out whether a given string is in the set is called matching. The result of such a matching is a Match
object, which evaluates to True
in Boolean context if the string is in the set.
A regex is typically constructed by a regex literal
rx/ ^ab /; # describes all strings starting with 'ab'/ ^ ab /; # samerx/ \d ** 2/; # describes all strings containing at least two digits
A named regex can be defined with the regex
declarator followed by its definition in curly braces. Since any regex does Callable
introspection requires referencing via &
-sigil.
my ;say .^name; # OUTPUT: «Regex»
To match a string against a regex, you can use the smartmatch operator:
my = 'abc' ~~ rx/ ^ab /;say .Bool; # OUTPUT: «True»say .orig; # OUTPUT: «abc»say .Str; # OUTPUT: «ab»say .from; # OUTPUT: «0»say .to; # OUTPUT: «2»
Or you can evaluate the regex in Boolean context, in which case it matches against the $_
variable
= 'abc';if / ^ab /else
Methods§
method ACCEPTS§
multi method ACCEPTS(Regex: Mu --> Match)multi method ACCEPTS(Regex: @)multi method ACCEPTS(Regex: %)
Matches the regex against the argument passed in. If the argument is Positional
, it returns the first successful match of any list item. If the argument is Associative
, it returns the first successful match of any key. Otherwise it interprets the argument as a Str
and matches against it.
In the case of Positional and Associative matches, Nil
is returned on failure.
method Bool§
multi method Bool(Regex: --> Bool)
Matches against the caller's $_ variable, and returns True
for a match or False
for no match.