0

Looking for regex that validates a name.

Valid name:

  • Barack Hussein Obama
  • Barack-Obama
  • Barack
  • aa
  • Aa
  • Abc
  • abc
  • abcd
  • ab
  • Barack-Obama
  • Barack
  • aa
  • Aa
  • Abc
  • abc
  • abcd
  • ab
  • crazybcÜüßäÄöÖàâäèéêëîï nameôœùûüÿçÀÂ-ÄÈÉÊËÎÏÔŒÙÛÜŸÇ
  • sÜüßäÄöÖàâäèéêëîïôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜΘεσσαλονίκηŸМоскваÇ

Not valid names are:

  • a-b
  • a
  • A
  • b
  • B
  • Barack Obama.
  • Simon.-#+;:_*´`?=)(/&%$§!123456789
  • Simon.-#+;:_*´`?=)(/&%$§!123456789
  • Donald B. Trump
  • D. B. Trump

I am very close to a correct regex:

^[^\W\d_]+(?:[-\s][^\W\d_]+)*?$

I am using python!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
PParker
  • 1,419
  • 2
  • 10
  • 25
  • 2
    Should there be 2 or more chars? `^[^\W\d_]{2,}(?:[-\s][^\W\d_]{2,})*?$` https://regex101.com/r/i6JaWX/1 – The fourth bird May 29 '20 at 14:17
  • Can you make an example please? Maybe there is sth, I didn't think of – PParker May 29 '20 at 14:41
  • @regex101: Yes, more then 2 chars! Your expression looks very good. Thanks a lot! – PParker May 29 '20 at 14:42
  • @Thefourthbird, that's worthy a post. @PParker, do you mind using `regex` module and switching from a negated character class to matching any kind of letter from any language? If not then: `^\p{L}{2,}(?:[- ]\p{L}{2,})*?$` – JvdV May 29 '20 at 15:01

0 Answers0