Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Stumped by regex

by reasonablekeith (Deacon)
on Dec 06, 2006 at 15:05 UTC ( [id://588102]=note: print w/replies, xml ) Need Help??


in reply to Stumped by regex

Does it _need_ to be one regex? You might be better of checking the length first? Something like this...?

UPDATE: See replies....

if (length($name) <= 20 and $name =~ m/^\w+\s(\w+\s)?\w+$/i) { print "okay\n"; }

By the way, your character restriction doesn't work because you can match up to three characters in one go within the parentheses, so your upper limit here is actually 60.

---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: Stumped by regex
by brickwall (Acolyte) on Dec 06, 2006 at 15:18 UTC
    Great, but wouldnt your regex also allow numbers to be entered, which i dont want Thanks
      yup, dumb mistake by me. Also though, I assumed there was a first name here too. So the above regex allows an additional space that it shouldn't. You're probably okay with just the following...
      $name =~ m/^[a-z]+\s?[a-z]+$/i
      ...although this does force a minimum length of 2.

      Having said this, if I'd seen JediWizard's reply, I wouldn't have posted mine. I think he nailed it first time (++).

      ---
      my name's not Keith, and I'm not reasonable.

        You might even prefer [[:alpha:]]+ instead of hard coding a-z in case you ever need to work in non-ASCII-betic locales.</pedant>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found