views:

628

answers:

3

I get a list of names and need to validate them. Some examples:

  • Elizabeth T. Bang
  • Elizabeth Bang
  • Zaki M. F. El-Adawy
  • joseph m. pastore, jr.
  • Zaki M. F. El-Adawy

How can I use regular expressions in Java to validate them?

+3  A: 

I'd start by listing the rules that dictate a valid name. Just in plain old english. I suspect you'll wind up with a very complicated set of rules and exceptions. Once you've figured that bit out you'll be ready to tackle writing a regex to match them.

Kris
I suspect that this attempt will prove enlightening.
Hank Gay
Yes, once you have the list, you'll decide that you can't validate them. See http://stackoverflow.com/questions/620118/personal-names-in-a-global-application-what-to-store, et. al.
John Saunders
+1  A: 

You might find the QuickREx Eclipse plugin to be helpful if you are using Eclipse. I use it any time I'm testing and developing complex regular expressions. It lets you specify test input, view all matching groups, etc.

Alex Miller
+2  A: 

Why do you need to validate names? To catch typos or people just mashing the keyboard? Is it really worth the risk to reject a legitimate customer with an unusual name? There are people with no middle name or more than one, people who only have one name altogether, people who insist on having their title used everywhere, and of course all kinds of non-ASCII characters.

Michael Borgwardt
Validating names is bad bad bad! My wife has 2 middle names, so most things reject her. There are also multiple word last names, etc. Depending on languages, you have all kinds of different rules... so unless you have a GREAT reason to validate names, i'd avoid it!
John Gardner