Home > front end >  Matching special character (#@#!~`%^&()[]}{;') and replace it with _ (underscore) in perl
Matching special character (#@#!~`%^&()[]}{;') and replace it with _ (underscore) in perl

Time:04-21

I want to remove all special characters except this 2 character .-

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

But the line above it not only removes the special character but also non-alphabet characters e.g Arabic or other none alphabet characters.

How to remove only these characters (#@#!~`%^&()[]}{;',)

CodePudding user response:


Building on Gene's comment, specify what you want to replace but I'd escape each special character. Note, to replace #, use \#\# in character array as shown in line 2:

$name = "@ # R ! ~## ` % ^ & ( O ){{();,'`@@ { } ;!!! ' N , ";
$name =~ s/[\@\!\~\`\%\&\^\(\)\{\}\;\'\,\#\#]//g;
$name =~ s/ *//g;
print $name; 
### Outputs RON
  •  Tags:  
  • perl
  • Related