Click here to Skip to main content
15,746,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I think I have most of the pattern. But need help allowing the [square brackets].

I want the pattern for surnames to be allowed if they have * or ? or ' or [] or MacD or St Jim or O'Brien or combination of any of these.

Example: O'Brien or St James or O'Bri*n or S* James or O'Bri*n? or St Ja*mes? or [O'Bri*n?] or [St *ames?]

I want to use this in a G-form

Thank you

What I have tried:

\w+?[']?[A-Za-z|*|?]{0,20}\s?[a-z A-Z|*|?]{0,20}
Updated 16-Dec-19 3:49am
F-ES Sitecore 16-Dec-19 9:50am    
Using regex to validate people's names is only going to end in tears.

1 solution

Your regex doesn't "allow" for quotes: it requires it.
Alphanumeric, one or more repetitions, as few as possible; followed by one character in this class: (quote only)
before continuing.
So it will match
But will never match
because the quote is missing.
In addition, adding '|' in a character class specification doesn't act as an "OR", it add the character '|' to the match list.

I think you are being a little overzealous here: English surnames can contain a wide range of silliness and it's probably better to just go "anything is allowed here" rather than trying to spend too much time rigidly validating it, as you are bound to forget someone. For example, my wife's maiden name was "Henderson-Allen" which wouldn't be allowed by your system, and there are many other oddities you will probably annoy people by rejecting if you aren't very careful.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900