Prev: Match Any character

Escape characters

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

An important change from the way the reular expressions worked in Perl 5 is that in Perl 6 any non-alphanumeric character needs to be escaped. Even if they don't currently have any special meaning or you'll get a "Statement not terminated properly" error during compilation. In a way this will make all the regexes look less clean as we will seen a lot more character escaping but it might force us to pick cleaner solution even if they are more wordy.

So for exmple we will have to escpe the = sign or the dash: - .

To allow further extension of the regexes Any other non-alnum character must be escaped:


use v6;

my $phone = "054-1234567";

if $phone ~~ m/ \d \- \d / {
    say "There are two numbers and a hyphen";

# or put in quotes:
if $phone ~~ m/ \d '-' \d / {
    say "Still two numbers and a hypen";

We will have to be very careful as there are going to be a number of cases that can easily trip up anyone who already uses regular expressions. For example the pound key # is now a special character by default meaning a comment.

So you'd better escape it when you really mean to match a # character.

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:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-01-01

Prev: Match Any character


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: 3.5984047

Perl 6 Tricks and Treats newsletter

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