pattern match of possibly "dangerous" strings

A

alexxx.magni

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
 
G

Gunnar Hjalmarsson

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
 
A

alexxx.magni

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

Alessandro
 
P

Paul Lalli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top