Regexes in Perl 6


You can still use m// as a regex but as you can see spaces in the regex are non significant. It is quite similar to the effect of /x option in PCRE.


In Perl 6 the smart match 
~~
 operator 
is used for regex matching.

For negative matching use the 
!~~
 operator.
examples/regex2/simple.p6
use v6;

my $text = "Learning regexes";
if $text ~~ m/ regex / {
	say "This is about regexes!";
}

if $text !~~ m/ foobar / {
    say "No foobar in $text";
}

Regex in Perl 6 disregard spaces by default. People who are used to the Perl 5 style regular expressions - which means basically every programming language that has a regular expression library - will usuall think as spaces being significant in the regular expressions. We will have to unlearn that and think about the individual bits and pieces that are the tokens we would like to match. Basically Perl 6 regexes work as if you always had the /x modifyer on which in Perl 5 means disregard spaces and treat # as start of comment.


Copyright 2006, 2007, 2008, 2009, 2010 Gabor Szabo http://szabgab.com/ Index | TOC
If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed