For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2007 > pattern match of possibly "dangerous" strings









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 pattern match of possibly "dangerous" strings
alexxx.magni@gmail.com

2007-06-28, 7:04 pm

Hi all,
I have a small program that crawls my filesystem, checking filenames
and doing useful stuff.
The heart of it, of course is a pattern match line, where the filename
under check is compared to a pattern.
Everything was running perfectly until I got:

/0ale/excursions/digital/manuals/O'Reilly Perl CD Bookshelf v2.0/
advprog/examples/Extending/Typemaps_with_XS/00comment.txt exists,
updating it

Nested quantifiers in regex; marked by <-- HERE in m/^\s+Car_c++ <--
HERE _obj ! (\S.*)/ at /0ale/excursions/digital/script/comment-dev
line 271.


(1st line is my prog. output, 2nd line is the error)
apart from the fun fact that the error came while crawling the Perl
Bookshelf, I immediately noticed that names containing special chars
(as that damn directory Car_c++_obj) can bring trouble.
How can I be sure that a generic string read in $_ can be safely put
in a RE, without its chars being misunderstood as RE special chars???

Thanks!

Alessandro Magni

Gunnar Hjalmarsson

2007-06-28, 7:04 pm

alexxx.magni@gmail.com wrote:
> I have a small program that crawls my filesystem, checking filenames
> and doing useful stuff.
> The heart of it, of course is a pattern match line, where the filename
> under check is compared to a pattern.
> Everything was running perfectly until I got:
>
> /0ale/excursions/digital/manuals/O'Reilly Perl CD Bookshelf v2.0/
> advprog/examples/Extending/Typemaps_with_XS/00comment.txt exists,
> updating it
>
> Nested quantifiers in regex; marked by <-- HERE in m/^\s+Car_c++ <--
> HERE _obj ! (\S.*)/ at /0ale/excursions/digital/script/comment-dev
> line 271.
>
>
> (1st line is my prog. output, 2nd line is the error)
> apart from the fun fact that the error came while crawling the Perl
> Bookshelf, I immediately noticed that names containing special chars
> (as that damn directory Car_c++_obj) can bring trouble.
> How can I be sure that a generic string read in $_ can be safely put
> in a RE, without its chars being misunderstood as RE special chars???


perldoc -f quotemeta

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
alexxx.magni@gmail.com

2007-06-28, 7:04 pm

thank you,
I didnt know about this particular kind of quote - it sertainly works
now with a simple $q=quotemeta($_);

Alessandro

On Jun 28, 5:06 pm, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
> alexxx.ma...@gmail.com wrote:
>
>
>
>
> perldoc -f quotemeta
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl



Paul Lalli

2007-06-28, 7:05 pm

On Jun 28, 11:37 am, "alexxx.ma...@gmail.com" <alexxx.ma...@gmail.com>
wrote:
> On Jun 28, 5:06 pm, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
[color=darkred]
> I didnt know about this particular kind of quote - it
> sertainly works now with a simple $q=quotemeta($_);


Read the doc Gunnar pointed you to again. It's simpler than that.
Just use the \Q (and possibly \E) characters within your regexp.

perldoc perlre

Paul Lalli

Sponsored Links







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

Copyright 2008 codecomments.com