Gossamer Forum
Home : General : Perl Programming :

What does this statement do?

Quote Reply
What does this statement do?
Hi,
Can someone translate to me what the following statement means:

$Username =~ s/\W//g;

For some reason, I think it's stripping out any characters that are not a to z for example, someon enters a username of suzy-q and the login to the system fails.

Is this because the above statement is stripping out any extra characters from the Username before processing?

Thanks,
Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: What does this statement do? In reply to
Yes. It strips out all non-alpha-numeric characters.

Alpha-numeric characters are 0 through 9, A through Z, a through z and _.


JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: What does this statement do? In reply to
Thanks,
Is there a way to strip out everything but a to z and - ?

Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: What does this statement do? In reply to
There's probably some really spiffy way to do it, but I don't know what it would be. You could possibly use a series of substitutions:

$Username =~ s/_//g; # strip out all underlines
$Username =~ s/-/_/g; # change all hyphens to underlines
$Username =~ s/\W//g; # strip out all non alpha-numeric characters
$Username =~ s/_/-/g; # change underlines back to hyphens
$Username =~ s/[0-9A-Z]//g; # strip out all digits and capital letters




JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: What does this statement do? In reply to
You can use:
$Username =~ s/[^a-z-]//g;

-- Gordon


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: What does this statement do? In reply to
See? I told you there was probably a spiffy way to do this. Smile

JPD
http://www.jpdeni.com/dbman/