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.


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