For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > regex quoting









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regex quoting
Jeremy Kister

2006-07-28, 9:56 pm

perldoc -q quote talks about \Q before a regex to escape special characters.

how do you use \Q when you want to anchor the regex with a dollar sign ?


my $string = my $regex = "foo";
print "match\n" if($string =~ /^\Q${regex}$/);



--

Jeremy Kister
http://jeremy.kister.net./
Paul Lalli

2006-07-28, 9:56 pm

Jeremy Kister wrote:
> perldoc -q quote talks about \Q before a regex to escape special characters.
>
> how do you use \Q when you want to anchor the regex with a dollar sign ?
>
>
> my $string = my $regex = "foo";
> print "match\n" if($string =~ /^\Q${regex}$/);


\Q, \L, and \U are all terminated by a \E

print "match\n" if $string =~ /^\Q$regex\E$/;

read about it in the various perldoc perlre* pages...

Paul Lalli

Tom Phoenix

2006-07-28, 9:57 pm

On 7/28/06, Jeremy Kister <perl-01@jeremykister.com> wrote:

> how do you use \Q when you want to anchor the regex with a
> dollar sign?


I think you're looking for \E, which "ends" the section of the string
quoted via \Q. Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Mumia W.

2006-07-28, 9:57 pm

On 07/28/2006 03:01 PM, Jeremy Kister wrote:
> perldoc -q quote talks about \Q before a regex to escape special
> characters.
>
> how do you use \Q when you want to anchor the regex with a dollar sign ?
>
>
> my $string = my $regex = "foo";
> print "match\n" if($string =~ /^\Q${regex}$/);
>
>
>


\E enables normal regex metacharacter usage:

/^\Qmystring\E$/

Rob Dixon

2006-07-28, 9:57 pm

Jeremy Kister wrote:
>
> perldoc -q quote talks about \Q before a regex to escape special
> characters.
>
> how do you use \Q when you want to anchor the regex with a dollar sign ?
>
> my $string = my $regex = "foo";
> print "match\n" if($string =~ /^\Q${regex}$/);


\E marks the end of the string you want escaping (without it all non-word
characters are escaped up to the end of the regex) so you can write:

print "match\n" if($string =~ /^\Q${regex}\E$/);

By the way you can miss out the curly brackets around the the scalar identifier
here as it's unambiguous:

print "match\n" if($string =~ /^\Q$regex\E$/);

but that isn't necessarily the case - it depends upon what appears after the
scalar in the regex.

HTH,

Rob


Timothy Johnson

2006-07-28, 9:57 pm

Use \E to stop the quoting.


-----Original Message-----
From: Jeremy Kister [mailto:perl-01@jeremykister.com]=20
Sent: Friday, July 28, 2006 1:01 PM
To: beginners@perl.org
Subject: regex quoting

perldoc -q quote talks about \Q before a regex to escape special
characters.

how do you use \Q when you want to anchor the regex with a dollar sign ?


my $string =3D my $regex =3D "foo";
print "match\n" if($string =3D~ /^\Q${regex}$/);

John W. Krahn

2006-07-28, 9:57 pm

Jeremy Kister wrote:
> perldoc -q quote talks about \Q before a regex to escape special
> characters.
>
> how do you use \Q when you want to anchor the regex with a dollar sign ?
>
>
> my $string = my $regex = "foo";
> print "match\n" if($string =~ /^\Q${regex}$/);



print "match\n" if $string =~ /^\Q$regex\E$/;



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com