Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex to replace unsafe chars?

Reply
Thread Tools

Regex to replace unsafe chars?

 
 
Bryan Coon
Guest
Posts: n/a
 
      03-03-2004
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

 
Reply With Quote
 
 
 
 
Jaap Karssenberg
Guest
Posts: n/a
 
      03-03-2004
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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-03-2004
Bryan Coon <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) (reverse each component for email address)
 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      03-03-2004
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

 
Reply With Quote
 
James Willmore
Guest
Posts: n/a
 
      03-03-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
visual studio 2005 unsafe code may only appear if compiling with /unsafe rockdale ASP .Net 3 11-03-2006 05:45 PM
unchecked or unsafe operations with JDK 1.5.0 Axl Java 2 09-01-2004 11:07 PM
Compiling a code behind file as unsafe Jeronimo Bertran ASP .Net 1 11-19-2003 03:08 AM
Unsafe Email attachments Ian Bromley Computer Support 23 08-02-2003 12:22 PM



Advertisments