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";
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.