Regex to match only letters

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

How can I write a regex that matches only letters?

20条回答
  •  一整个雨季
    2020-11-22 16:41

    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.

提交回复
热议问题