Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: regex: only want [a-zA-Z] and comma chars in a string (Don't use a regex!)

by dragonchild (Archbishop)
on Oct 12, 2003 at 19:22 UTC ( [id://298699]=note: print w/replies, xml ) Need Help??


in reply to regex: only want [a-zA-Z] and comma chars in a string

Try this:
sub is_valid_string { my $string = shift; # Handle the empty case return 0 unless length $string; # Preserve trailing empty fields my @string = split ',', $string, -1; # No commas at the beginning, end, or next to one another. return 0 if grep { !$_ } @string; return 0 if grep { /[^a-zA-Z]/ } @string; return 1; }

It was tested with your test strings and passed with flying colors.

------
We are the carpenters and bricklayers of the Information Age.

The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://298699]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found