Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Regular Expression Question

by !1 (Hermit)
on Dec 04, 2003 at 19:33 UTC ( #312289=note: print w/replies, xml ) Need Help??


in reply to Regular Expression Question

It would probably be far easier to first verify that the string contains legitimate characters and then check whether or not any of the conditions that would make it illegal exist.

#!/usr/bin/perl -wl use strict; for (<DATA>) { chomp; if (! /[^a-zA-Z0-9,]/ and ! /^,|,{2}|,$/) { print "Yep: $_"; } else { print "Nope: $_"; } } __DATA__ this,should,work,11 ,this,should,not,work this,,too,should,not,work nor,this,one,

Please please please read perldoc perlretut and perldoc perlre for a deeper understanding of regular expressions.

Replies are listed 'Best First'.
Re: Re: Regular Expression Question
by TASdvlper (Monk) on Dec 04, 2003 at 20:12 UTC
    Thanks for your help. Why is there a "!" in front of the 1st regexp ? I understand the 2nd.
      The character class is negated, note where the ^ is...
      ! /[^a-zA-Z0-9,]/
      ...however I'm not sure the double negative is faster enough to warrant not just doing the more obvious...
      /^[a-zA-Z0-9,]+$/
      ...it's also not stated whether /^$/ is valid or not, which is different for the above two.
      --
      James Antill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://312289]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2021-09-18 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?