0

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?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • Could describe the rules for good names? – Kees de Kooter Mar 23 '09 at 10:26
  • If you have a single string / text field / whatever with entire names in them, don't expect to be able to parse them out into first, middle, last reliably. In various cultures, you'll encounter no/multiple middle names, last names before first, hyphens or even spaces in first/last names… – Ben Blank Mar 23 '09 at 16:19

3 Answers3

3

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
  • 14,426
  • 7
  • 55
  • 65
  • 1
    I suspect that this attempt will prove enlightening. – Hank Gay Mar 23 '09 at 10:41
  • 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 Mar 23 '09 at 10:45
3

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
  • 342,105
  • 78
  • 482
  • 720
  • 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 Mar 23 '09 at 18:39
1

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
  • 69,183
  • 25
  • 122
  • 167