Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

by nevyn (Monk)
on Oct 14, 2003 at 19:39 UTC ( #299234=note: print w/replies, xml ) Need Help??

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'.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://299234]
choroba makes the l a r g e integration test pass!
[Discipulus]: congrats both!

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-12-18 10:15 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (476 votes). Check out past polls.