Camelia
Prev: Match Any character

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.
=title Escape characters =timestamp 2012-01-01T01:02:05 =author szabgab =keywords \-, \=, \, \\, '-', '=' =status show

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:

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


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.

Prev: Match Any character

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

Perl 6 Tricks and Treats newsletter

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