tutorial/regex2/named_regex.p6
use v6; my $phone = "054-1234567"; # You can also create named regexes: my regex prefix { \d ** 2..3 } my regex number { <+digit - [0]> \d ** 6 } # if $phone ~~ m/^ <prefix> '-' <number> $/ { # but this does not work now so we write the full syntax: if $phone ~~ m/^ <prefix=&prefix;> '-' <number=&number;> $/ { say "prefix $/<prefix> number: $/<number>"; } # as you can see $/ is also a hash
let's create the named regexes for the word and duplicate:
tutorial/regex2/named_regex2.p6
use v6; my $s = 'The elvish brown fox jumped over the the lazy dog'; my regex word { \w+ [ \' \w+]? } my regex dup { << <word=&word;> \W+ $<word> >> } if $s ~~ m/ <dup=&dup;> / { say "Found '{$<dup><word>}' twice in a row"; }
Published on 2012-01-01