RegEx for the name: any language, but the first letter must be capital

I have a requirement to accept the first name as input and check that the first letter is a header and that there can be 1 space after the end of the line.

This RegEx works for 'Bob':

^[A-Z][A-Za-z\p{L}]+[\s,.'\-]?[a-zA-Z\p{L}]*$

An additional requirement is to allow any language / character, which then includes the ability to use unicode.

This RegEx works for the Russian name: 'Athanasius'

^[A-Z\p{L}][A-Za-z\p{L}]+[\s,.'\-]?[a-zA-Z\p{L}]*$

... However, although it allows the use of unicode, it also allows me to enter "bob" with the first small letter, and RegEx allows this through.

Is there a way to allow both unicode and still mark the first letter when it is not capital? (Using RegEx)

, , RegEx .

+4
1

​​ \p{Lu}.

^\p{Lu}\p{L}+[\s,.'\-]?\p{L}*$

^\p{Lu}\p{L}+(?:[\s,.'-]\p{L}+)?$

regex demo 1 - regex 2, , , .. ( [\s,.'-]).

: [A-Za-z\p{L}], \p{L} [a-zA-Z].

:

  • ^ -
  • \p{Lu} -
  • \p{L}+ - Unicode
  • (?:[\s,.'-]\p{L}+)? - ()
    • [\s,.'-] - , ,, ., '
    • \p{L}+ - 1 Unicode
  • $ - .
+4

Source: https://habr.com/ru/post/1655171/


All Articles