Regex to replace unsafe chars?

Discussion in 'Perl Misc' started by Bryan Coon, Mar 3, 2004.

  1. Bryan Coon

    Bryan Coon Guest

    I need to remove unsafe characters from a filename, and replace them all
    with underscores.

    The characters I need to search for and replace are:
    !@#$%^*()'"{}[]<>

    How do I search and replace for any of the group of special characters
    like this?

    I tried $filename =~ s/[\!\@\#\$\%\^\*\(\)\{\}\[\]\'\"\<\>]/_/g;
    But I dont think they all need to be escaped and Im not sure this is the
    right format anyways.

    Thanks,
    B
    Bryan Coon, Mar 3, 2004
    #1
    1. Advertising

  2. On Wed, 03 Mar 2004 01:03:12 GMT Bryan Coon wrote:
    : I need to remove unsafe characters from a filename, and replace them
    : all with underscores.

    maybe it would be better to define the save characters, say a filename
    should only contain letters, digits, '.' and '-' then you could do this:

    $filename =~ s/[^\w\.\-]/_/g;

    --
    ) ( Jaap Karssenberg || Pardus [Larus] | |0| |
    : : http://pardus-larus.student.utwente.nl/~pardus | | |0|
    ) \ / ( |0|0|0|
    ",.*'*.," Proud owner of "Perl6 Essentials" 1st edition :) wannabe
    Jaap Karssenberg, Mar 3, 2004
    #2
    1. Advertising

  3. Bryan Coon <> wrote in news:k9a1c.19403$zH5.18179
    @newssvr29.news.prodigy.com:

    > I need to remove unsafe characters from a filename, and replace them
    > all with underscores.


    You should decide what is safe and only allow those characters instead of
    trying to figure out what is not safe.

    Sinan.

    --
    A. Sinan Unur
    (reverse each component for email address)
    A. Sinan Unur, Mar 3, 2004
    #3
  4. Bryan Coon

    Bob Walton Guest

    Bryan Coon wrote:

    > I need to remove unsafe characters from a filename, and replace them all
    > with underscores.
    >
    > The characters I need to search for and replace are:
    > !@#$%^*()'"{}[]<>
    >
    > How do I search and replace for any of the group of special characters
    > like this?
    >
    > I tried $filename =~ s/[\!\@\#\$\%\^\*\(\)\{\}\[\]\'\"\<\>]/_/g;
    > But I dont think they all need to be escaped and Im not sure this is the
    > right format anyways.

    ....
    > B
    >


    This is probably best done with the translate operator [untested]:

    $filename=~tr/!@#$%^*()'"{}[]<>/_/;

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Mar 3, 2004
    #4
  5. On Wed, 03 Mar 2004 02:10:28 +0100, Jaap Karssenberg wrote:

    > On Wed, 03 Mar 2004 01:03:12 GMT Bryan Coon wrote:
    > : I need to remove unsafe characters from a filename, and replace them
    > : all with underscores.
    >
    > maybe it would be better to define the save characters, say a filename
    > should only contain letters, digits, '.' and '-' then you could do this:
    >
    > $filename =~ s/[^\w\.\-]/_/g;


    I'll go one better and direct the OP to a CERT page on exactly this idea -
    as applied to untainting variables in CGI scripts ....

    http://www.cert.org/tech_tips/cgi_metacharacters.html

    HTH

    --
    Jim

    Copyright notice: all code written by the author in this post is
    released under the GPL. http://www.gnu.org/licenses/gpl.txt
    for more information.

    a fortune quote ...
    Elevators smell different to midgets
    James Willmore, Mar 3, 2004
    #5
    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. rockdale
    Replies:
    3
    Views:
    3,260
    rockdale
    Nov 3, 2006
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,285
    Tim Rentsch
    Sep 23, 2005
  3. Replies:
    3
    Views:
    758
    Reedick, Andrew
    Jul 1, 2008
  4. Hongyu
    Replies:
    9
    Views:
    902
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    226
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page