homepage Welcome to WebmasterWorld Guest from 54.197.108.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Replace non word characters EXCEPT hyphen
StaceyJ




msg:4243198
 4:34 pm on Dec 16, 2010 (gmt 0)

I'm trying to allow the use of the hyphen (dash) in a text input, but I can't get it to work. I've tried the following with no success:

$string =~ s/[^\w|-]//g;
$string =~ s/[^\w\-]//g;
$string =~ s/[^a-zA-Z0-9-]//g;

I've also tried the ascii code - with and without escaping the & and ;

Any help would be greatly appreciated!

 

janharders




msg:4243384
 11:36 pm on Dec 16, 2010 (gmt 0)

\w is a char-group itself, so depending on the locale you are working with, it's something like [-a-zA-Z]

the hyphen is a special char in groups, and you'll have to escape it, unless it's the first char, so

$string =~ s/[^a-zA-Z0-9-]//g;
should be
$string =~ s/[^a-zA-Z0-9\-]//g;
rocknbil




msg:4243692
 4:56 pm on Dec 17, 2010 (gmt 0)

Right, unless escaped the hyphen represents a range. The above is equivalent to

$string =~ s/[^a-z\d\-]//ig;

with the i modifier (case insensitive) and \d, which is any digit. :-)

StaceyJ




msg:4243710
 5:25 pm on Dec 17, 2010 (gmt 0)

Thanks both of you! I appreciate it. I thought it (the hyphen) needed to be escaped, but that wouldn't work either until I found another spot in the code about 1000 lines down that was preventing it from working. I have it working now. Thanks also for the shortcut example!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved