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

by mshiltonj (Sexton)
on Oct 13, 2003 at 22:02 UTC ( #298957=note: print w/replies, xml ) Need Help??


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

This example...
-----
#!/usr/bin/perl -w use strict; my %commas = ( 'notalot' => 1, 'england,rugby' => 1, 'chicken,egg,duck,feet' => 1, ',southampton' => 0, 'bristol,' => 0, 'iran,,canada,france' => 0, ); foreach my $key (keys %commas) { unless (($key =~ /^,|,,|,$/)) { print "PASS"; } else { print "FAIL"; } print ": $key\n"; }
----
... seems to work for me. This was it's output:
FAIL: bristol, PASS: notalot PASS: england,rugby FAIL: iran,,canada,france PASS: chicken,egg,duck,feet FAIL: ,southampton

Replies are listed 'Best First'.
Re: Re: regex: only want [a-zA-Z] and comma chars in a string
by dragonchild (Archbishop) on Oct 13, 2003 at 23:31 UTC
    Ahhh ... the pitfalls of coding to the test cases instead of the specification. The OP specifically stated that he needed the characters to be in the class [a-zA-Z], which your code doesn't check for. Try the following string "1", which should fail.

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

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

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

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-12-15 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (443 votes). Check out past polls.

    Notices?