Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://298699]
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-07-22 07:13 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (452 votes). Check out past polls.