Camelia
Prev: Perl 6 Regexes

Regexes in Perl 6

Note! This site is about Perl 6.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.

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.

tutorial/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.


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-01-01


Prev: Perl 6 Regexes

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 2.649025

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: