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

how to match string?

Status
Not open for further replies.

michael12

Programmer
Sep 26, 2002
25
0
0
US
If I want to filter the string which has only character(A-z) or number (0-9), and ignore all the others, how can I do that?
for example: valid string is: aVc, a55,Zf2,4r4
invalid: a$g,4_y
 
The regex character class \w matches what you want except for the underscore char, '_', so if you match [tt][\W_][/tt], which is non \w characters plus the underscore then that is what you want to exclude. So any string that contains only the negation of that class is a valid string. (follow? I don't.) Here's the regex:
Code:
     if ( $string =~ /^[^\W_]+$/ ) {
        
        # Do what you want with the valid string here
        print "Good: $string\n";

    }

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top