Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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 ( #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.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://298699]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-11-18 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (277 votes). Check out past polls.

    Notices?