Regex to match only letters

后端 未结 20 1566
孤城傲影
孤城傲影 2020-11-22 16:24

How can I write a regex that matches only letters?

20条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 16:44

    Use a character set: [a-zA-Z] matches one letter from A–Z in lowercase and uppercase. [a-zA-Z]+ matches one or more letters and ^[a-zA-Z]+$ matches only strings that consist of one or more letters only (^ and $ mark the begin and end of a string respectively).

    If you want to match other letters than A–Z, you can either add them to the character set: [a-zA-ZäöüßÄÖÜ]. Or you use predefined character classes like the Unicode character property class \p{L} that describes the Unicode characters that are letters.

提交回复
热议问题