Rules / Types, classes & roles / domain-specific

Regex Reference

String pattern

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method ACCEPTS #

multi method ACCEPTS(Regex:D: Mu --> Match:D)
multi method ACCEPTS(Regex:D: @)
multi method ACCEPTS(Regex:D: %)

Returns Match:D.

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

method Bool #

multi method Bool(Regex:D: --> Bool:D)

Returns Bool:D.

Matches against the caller's $_ variable, and returns True for a match or False for no match.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

3 no-output · 2 ok

class Regex is Method { }

Not executed: the documentation states no expected output for this example.

rx/ ^ab /;      # describes all strings starting with 'ab'
/ ^ ab /;       # same
rx/ \d ** 2/;   # describes all strings containing at least two digits

Not executed: the documentation states no expected output for this example.

my regex R { \N };
say &R.^name; # OUTPUT: «Regex␤»
Output
Regex

Documentation, Rakudo and Raku++ all agree.

my $match = 'abc' ~~ rx/ ^ab /;
say $match.Bool;                # OUTPUT: «True␤»
say $match.orig;                # OUTPUT: «abc␤»
say $match.Str;                 # OUTPUT: «ab␤»
say $match.from;                # OUTPUT: «0␤»
say $match.to;                  # OUTPUT: «2␤»
Output
True
abc
ab
0
2

Documentation, Rakudo and Raku++ all agree.

$_ = 'abc';
if / ^ab / {
    say '"abc" begins with "ab"';
}
else {
    say 'This is a weird alternative Universe';
}

Not executed: the documentation states no expected output for this example.