Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^3: Stumped by regex

by reasonablekeith (Deacon)
on Dec 06, 2006 at 15:25 UTC ( #588110=note: print w/replies, xml ) Need Help??

in reply to Re^2: Stumped by regex
in thread Stumped by regex

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.

Replies are listed 'Best First'.
Re^4: Stumped by regex
by Fletch (Bishop) on Dec 06, 2006 at 15:32 UTC

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

      You beat me to it. I saw [a-zA-Z] and thought [[:alpha:]] might be shorter (it's not, due to the colons and extra set of brackets). Then I saw [a-z] with /i which blew it away. But [[:alpha:]] is more portable.

      Also, isn't OP talking about surnames only? I believe surname means last name. So spaces are rare special instances that must be dealt with, not a requirement. (In my area, it's far more common to see single word last names, then in multi-word last names, hyphens are more common delimiters than spaces, so you might want to include hyphens in your regex as well)

      So if the regex sees "Larry Wall," it thinks that is someone's LAST name only, as "Larry Wall, Joe" if you're doing Last, First. If it sees "GeorgeWashington," that's just a last name, etc.

      The main thing I would add is that unless you're sure there will be no hyphenated names (perhaps you're instructing users to enter spaces in place of hyphens?), I'd include a hyphen in addition to a space.

      If you want to be really robust, you could check for capitalization only at the right places (after space, Hyphen, or a lower-case 'c' at either the second letter in a word that starts with 'M' or the third letter in a word that starts with 'Ma.' Also, in your spec you specify a maximum of one space; I don't know if there are double-hyphenated surnames out there.


      I like computer programming because it's like Legos for the mind.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2022-08-18 04:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found