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.
| [reply] [d/l] |
| [reply] [d/l] |
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.
| [reply] |