in reply to Re: regex: only want [a-zA-Z] and comma chars in a string
in thread regex: only want [a-zA-Z] and comma chars in a string
unless (($key =~ /^,|,,|,$/))
Cute, testing for the negative is much more readable here ... but probably something I wouldn't have tried. You need to check for the characters being in the valid class though (and I'd also put brackets around the anchors) so that would make it...
unless (($key =~ /(?:^,)|,,|[^a-zA-Z,]|(?:,$)))
Which isn't quite as nice anymore :(. So I'd probably still use the "obvious" non-negative...
if (($key =~ /^[a-zA-Z]+(?:,[a-zA-Z]+)*$))
Or if I was feeling really special, I might even do...
my $field = qr([a-zA-Z]+); if (($key =~ /^$field(?:,$field)*$))
...which is slightly more readable IMO.
update (broquaint): changed <pre> to <code> tags
|
|---|
| Replies are listed 'Best First'. |
|---|