Search code examples
regexvalidationalphanumeric

Improving the below RegEx for US and UK Names


I'm looking to validate English names. How could I improve this RegEx in order to ensure that the hyphen, comma or apostrophe does not come first or last in the string? Further, a hyphen or apostrophe must always be directly preceded and succeeded with a letter.

I have the RegEx: [a-zA-Z-', ].

My understanding is that this will allow for all letters, in all cases, hyphens, commas and apostrophes along with whitespace at any location in the string.

Quite new to the language so just getting my head around it.

Thanks.


Solution

  • Ok, since it's only given a presumed name, here's what you might want to try:

    ^[A-Z](['-]?[A-Za-z])*(,? [A-Z](['-]?[A-Za-z])*)*$
    

    This will work with names like O'Harry Jefferson-Wayne, but will reject words not ending with a small English letter. And it will also accept one-letter names, which do exist. "O" is a real name that Chinese immigrants in the US have a problem with as a lot of software rejects it for being too short.

    The gist of it is this [A-Z] start of name, optional dash or apostrophe, then continuation [A-Za-z] continuation. Then a repeatable group of optional names, with an optional comma delimiter, but required space. It will reject names with Umlauts or other non-english variants of letters.

    I took more time writing the examples to test at http://www.regexplanet.com/. That's where I practice my regular expressions when I'm developing. You should try it.