pattern match of possibly "dangerous" strings

Discussion in 'Perl Misc' started by alexxx.magni@gmail.com, Jun 28, 2007.

  1. Guest

    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
    , Jun 28, 2007
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Jun 28, 2007
    #2
    1. Advertising

  3. Guest

    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 <> wrote:
    > 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
    , Jun 28, 2007
    #3
  4. Paul Lalli Guest

    On Jun 28, 11:37 am, "" <>
    wrote:
    > On Jun 28, 5:06 pm, Gunnar Hjalmarsson <> wrote:
    > > perldoc -f quotemeta


    > 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
    Paul Lalli, Jun 28, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    636
  2. Victor
    Replies:
    2
    Views:
    640
    Victor
    May 17, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    755
    Malcolm
    Jun 24, 2006
  4. anonym
    Replies:
    1
    Views:
    1,013
    Knute Johnson
    Jan 15, 2009
  5. seven.reeds
    Replies:
    9
    Views:
    152
    Uri Guttman
    Aug 10, 2006
Loading...

Share This Page