We are Borg We are Borg - 12 days ago 5
Java Question

Java, Regex : Adding umlaut and other german characters for first-name

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.

Answer

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}'-]+)*"
Comments