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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deciphering s/ statement

Status
Not open for further replies.

rico7474

MIS
Oct 29, 2001
9
0
0
CA
Hello all,

I'm just trying to look over a perl script and am having trouble with figuring out what this line would produce:

$var =~ s/[^~a-zA-Z-']//g;

The best I can come up with is it will find something that does not start with '~', or any letter, or ...(I'm not sure), and substitute it with nothing.

This is part of a short subroutine to verify that all characters in $var are proper for our name search program.

Any help you could give would be appreciated.
Thanks in advance.
Scott.
 
You were correct, it examines $var, and substitutes anything that is NOT a:
~
upper or lower case letter
- [dash]

with nothing. It's essentially filters out any character NOT specified in the character class defined by the [] brackets. It's called a negated character class since the first character in there is a carat (^). So it's not matching the beginning of the string, but it's there to NEGATE the character class.


Hope this helps,

--Jim
 
Oops, I missed the apostraphe in there, it also allows apostrophe's to be inside $var.

--Jim
 
Thanks Jim, that does help. I was looking at the last dash as a range operator and got stumped there. I wasn't sure if you needed to escape the dash.

Cheers.
 
If the dash is the last character, it is interpreted as just a plain old dash. If you want to put the dash in the middle somewhere, you could escape it and achieve the same result.

Glad I could help :)

--Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top