Regex to match only letters

2018-12-31 18:35发布

问题:

How can I write a regex that matches only letters?

回答1:

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.



回答2:

\\p{L} matches anything that is a Unicode letter if you\'re interested in alphabets beyond the Latin one



回答3:

Depending on your meaning of \"character\":

[A-Za-z] - all letters (uppercase and lowercase)

[^0-9] - all non-digit characters



回答4:

The closest option available is

[\\u\\l]+

which matches a sequence of uppercase and lowercase letters. However, it is not supported by all editors/languages, so it is probably safer to use

[a-zA-Z]+

as other users suggest



回答5:

/[a-zA-Z]+/

Super simple example. Regular expressions are extremely easy to find online.

http://www.regular-expressions.info/reference.html



回答6:

For PHP, following will work fine

\'/^[a-zA-Z]+$/\'


回答7:

Regular expression which few people has written as \"/^[a-zA-Z]$/i\" is not correct because at the last they have mentioned /i which is for case insensitive and after matching for first time it will return back. Instead of /i just use /g which is for global and you also do not have any need to put ^ $ for starting and ending.

/[a-zA-Z]+/g
  1. [a-z_]+ match a single character present in the list below
  2. Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed
  3. a-z a single character in the range between a and z (case sensitive)
  4. A-Z a single character in the range between A and Z (case sensitive)
  5. g modifier: global. All matches (don\'t return on first match)


回答8:

You would use

/[a-z]/gi

[]--checks for any characters between given inputs

a-z---covers the entire alphabet

g-----globally throughout the whole string

i-----getting upper and lowercase



回答9:

Just use \\w or [:alpha:]. It is an escape sequences which matches only symbols which might appear in words.



回答10:

Use character groups

\\D

Matches any character except digits 0-9

^\\D+$

See example here



回答11:

If you mean any letters in any character encoding, then a good approach might be to delete non-letters like spaces \\s, digits \\d, and other special characters like:

[!@#\\$%\\^&\\*\\(\\)\\[\\]:;\'\",\\. ...more special chars... ]

Or use negation of above negation to directly describe any letters:

\\S \\D and [^  ..special chars..]

Pros:

  • Works with all regex flavors.
  • Easy to write, sometimes save lots of time.

Cons:

  • Long, sometimes not perfect, but character encoding can be broken as well.


回答12:

You can try this regular expression : [^\\W\\d_] or [a-zA-Z].



回答13:

Java:

String s= \"abcdef\";

if(s.matches(\"[a-zA-Z]+\")){
     System.out.println(\"string only contains letters\");
}


回答14:

pattern = /[a-zA-Z]/

puts \"[a-zA-Z]: #{pattern.match(\"mine blossom\")}\" OK

puts \"[a-zA-Z]: #{pattern.match(\"456\")}\"

puts \"[a-zA-Z]: #{pattern.match(\"\")}\"

puts \"[a-zA-Z]: #{pattern.match(\"#$%^&*\")}\"

puts \"[a-zA-Z]: #{pattern.match(\"#$%^&*A\")}\" OK



回答15:

Pattern pattern = Pattern.compile(\"^[a-zA-Z]+$\");

if (pattern.matcher(\"a\").find()) {

   ...do something ......
}