Regex to match only letters

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

How can I write a regex that matches only letters?

相关标签:
20条回答
  • 2020-11-22 16:43

    In python, I have found the following to work:

    [^\W\d_]
    

    This works because we are creating a new character class (the []) which excludes (^) any character from the class \W (everything NOT in [a-zA-Z0-9_]), also excludes any digit (\d) and also excludes the underscore (_).

    That is, we have taken the character class [a-zA-Z0-9_] and removed the 0-9 and _ bits. You might ask, wouldn't it just be easier to write [a-zA-Z] then, instead of [^\W\d_]? You would be correct if dealing only with ASCII text, but when dealing with unicode text:

    \W

    Matches any character which is not a word character. This is the opposite of \w. > If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_].

    ^ from the python re module documentation

    That is, we are taking everything considered to be a word character in unicode, removing everything considered to be a digit character in unicode, and also removing the underscore.

    For example, the following code snippet

    import re
    regex = "[^\W\d_]"
    test_string = "A;,./>>?()*)&^*&^%&^#Bsfa1 203974"
    re.findall(regex, test_string)
    

    Returns

    ['A', 'B', 's', 'f', 'a']
    
    0 讨论(0)
  • 2020-11-22 16:43
    Pattern pattern = Pattern.compile("^[a-zA-Z]+$");
    
    if (pattern.matcher("a").find()) {
    
       ...do something ......
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 16:44

    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)
    0 讨论(0)
  • 2020-11-22 16:45

    Java:

    String s= "abcdef";
    
    if(s.matches("[a-zA-Z]+")){
         System.out.println("string only contains letters");
    }
    
    0 讨论(0)
  • 2020-11-22 16:45

    For PHP, following will work fine

    '/^[a-zA-Z]+$/'
    
    0 讨论(0)
提交回复
热议问题