Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove unwanted characters from string

Status
Not open for further replies.

akelabanda

Programmer
Dec 19, 2001
61
IN
Hi Guys

I have to remove any characters that do not match a SPACE, a-z, A-Z, 0-9 from a line of record that resembles the one below. Here, I have to remove the double quote and retain the rest of the text in the third comma separated field.

255,95017,D0 Ayu"Gila,255

But, I'm looking for a generic reg expression which removes all unwanted characters that are not a SPACE, a-z, A-Z, 0-9.

I've tried doing -
1. $elements[38]=~s/\W//;
2. $elements[38]=~s/![0-9|a-z|A-Z]]//;

It doesn't seem to work.

Any help would be appreciated.

Thanks
Raj


UK's best mobile deals online
 
Firstly, to explain why what you're already trying doesn't work:

By default, when you're doing a substitution using s///, it will only make a single substitution. With the first regexp you've posted, you're probably seeing that the first non-word character is removed (BTW \W will replace spaces and will not replace underscores, so it's not quite what you're looking for).

With the second one, you have the same problem. However you're also misunderstanding a couple of other things. The exclamation mark is not a special character by itself in a regexp and so that will try to match a literal exclamation mark. Secondly, when you're inside a character class (i.e. between square brackets), the pipe character doesn't have any special meaning either, so that class is looking for anything in the ranges 0-9, a-z, A-Z or a pipe character.

Generally, when replacing single characters, it's more efficient to use a transliteration than a regexp anyway. This will do what you're looking for:
Code:
$elements[38] =~ tr/a-zA-Z0-9 //cd;
 
Not for me:
Code:
#!/usr/bin/perl -w
use strict;

my $str = '255,95017,D0            Ayu"Gila,255';

$str =~ tr/a-zA-Z0-9 //cd;

print $str,"\n";
 
Hi Ishnid

Thank you very much mate. It did work. There was another problem though. Originally my script was treating a file with header and trailer. But the one I was using now dint have them. That's another issue to deal with.

Thanks again for your help.

Cheers
Rajeev

UK's best mobile deals online
 
I'm not sure I understand what your other problem is. Can you elaborate further?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top