Reputation: 5311
I want to validate first-name sent by user with Regex. I found multiple expressions for first-name, but I also want to add german characters like äöüß
and french ones à À è È é É ù Ù ì Ì ò Ò ñ Ñ
to it. I tried regex evaluator suggested by SO here, but that didn't help. Wheneever I would make an extra square bracket, it would tell me Your regular expression does not match the subject string.
. What am I doing wrong?
Current Regex pattern :
^([A-Z][a-z]*((\s)))+[A-Z][a-z][äöüß]*$
Thank you.
Upvotes: 1
Views: 11243
Reputation: 1583
I agree with previous answer from Wiktor.
But to answer why your regex did not work:
You expected a capital letter followed by 0 or more letters and a space and then another word. And that other word is 1 cap. letter, 1 letter and then 0 or more Umlauts. I do not know any name that would fit. All those special characters should be in the same [] as the letters.
Also hyphenated names are not allowed.
So your regex could look more like this:
^([A-ZÜÄÖ][a-züäöß]*(\s|-))*[A-ZÜÄÖ][a-züäöß]*$
Upvotes: 3
Reputation: 626929
It is not a good idea to restrict people's names too much, I suggest a rather generic regex:
s.matches("(?U)[\\p{L}\\p{M}\\s'-]+")
This regex will match a string only consisting of 1 or more Unicode letters, diacritics, whitespaces, apostrophes or hyphens.
If you need more restrictive checks, like a whitespace may only appear inside the string and only if not consecutive, use grouping:
"(?U)[\\p{L}\\p{M}'-]+(?:\\s[\\p{L}\\p{M}'-]+)*"
Upvotes: 10