Camelia
Prev: Match Any character

Escape characters

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:

examples/regex2/escape_characters.p6
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.

Prev: Match Any character

Table of Contents

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

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