1

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.

6
  • 2
    Do not restrict names that hard. I'd rather suggest a very generic one: (?U)[\p{L}\p{M}\s'-]+, use it with String#matches(). A bit more complex: (?U)[\p{L}\p{M}'-]+(?:\s[\p{L}\p{M}'-]+)?, or (?U)[\p{L}\p{M}]+(?:[\s'-][\p{L}\p{M}]+)*, etc. Nov 23, 2016 at 12:34
  • @WiktorStribiżew : Can you explain me the regex you posted. Thank you. Nov 23, 2016 at 12:36
  • 1
    \p{L} - any Unicode letter. \p{M} - any diacritic. \s - whitespace, ' and - are literal single quote and -. Nov 23, 2016 at 12:37
  • @WiktorStribiżew : One of our crazy developers was trying some ascii art for names. Will this regex be sufficient to stop it? Nov 23, 2016 at 12:39
  • Well, it is difficult to tell, ASCII art is rather a free from of text. I think one can draw with letters as well. You should come up with some specs then to define the name constraints. However, it is not good idea to validate human names at all. Nov 23, 2016 at 12:42

2 Answers 2

10

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}'-]+)*"
3
  • Hi, Can you tell me how I can add numbers to this? Also, I have never seen such regex, is this some different type?Thank you. Feb 3, 2017 at 9:49
  • Number Unicode category class is \p{N} (or a more strict, \p{Nd}). So, just add this after \p{M} and that is all there needs to be done. Another idea: "(?U)[-\\w\\p{M}\\s'&&[^_]]+" (where you may remove &&[^_] if you also want to allow _). Feb 3, 2017 at 9:53
  • Thanks, I had added Nd, based upon Intellij suggestions, changed to N. Found an awesome tut on regular-expressions.info/unicode.html . Thank you. Feb 3, 2017 at 9:57
3

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üäöß]*$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.