0

Need to check with a regular expression that the last name consists only of letters. The alphabet can be any. That is, need something like /[[:alpha:]]+/g which is only for Latin, but for any alphabet. For example Крылов.

What does such a regular expression look like?

6
  • 1
    Does this help? stackoverflow.com/a/6068190/1377097 Commented Feb 27 at 19:50
  • Which regex implementation are you using? Commented Feb 27 at 19:57
  • 1
    if you know the character ranges for the letters in the alphabets you are targeting, you can use something similar to this answer: stackoverflow.com/a/150078/212869 Commented Feb 27 at 19:58
  • \A\pL+\z works. thanks, @beaker Commented Feb 27 at 20:09
  • As of Unicode 15.1 there are 136,762 alpha characters. [[:alpha:]] is a Posix class and \p{L} is a property class. Both match the exact same thing. Weather both, either, or none are supported in your regex engine of choice has to be verified. Commented Feb 28 at 23:14

1 Answer 1

3

That is, need something like /[[:alpha:]]+/g which is only for Latin...

That is mistaken.

[:alpha:] is a POSIX character class. Depending on your regex implementation what alpha matches either depends on your locale, or it matches all Unicode "letters".

If your regex implementation follows the Unicode conventions then the alpha character class is upper + lower. upper matches all Unicode upper case characters and lower matches all Unicode lower case characters.

POSIX character classes pre-date Unicode and their implementation can be inconsistent. If you want to be more explicit, some regex implementations provide Unicode character classes, usually as \p{xx}. You want \p{L} for all Unicode letters.

Because some languages have no concept of case, such as Japanese, some implementations, such as Ruby, will also include "other letters". They make [:alpha:] and \p{L} equivalent.

"ふ".match?(/[[:alpha:]]/) # true "ふ".match?(/\p{L}/) # true "a".match?(/[[:alpha:]]/) # true "a".match?(/\p{L}/) # true "Ⳃ".match?(/[[:alpha:]]/) # true "Ⳃ".match?(/\p{L}/) # true "1".match?(/[[:alpha:]]/) # false "1".match?(/\p{L}/) # false 

See regular-expressions.info's articles on POSIX Bracket Expressions and Unicode.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.