Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to match only letters in a regex?

I would really like to use \w but it also matches underscores so I'm going with [A-Za-z] which feels unnecessarily verbose and America centric. Is there a better way to do this? Something like [\w^_] (I doubt I got that syntax right)?

like image 985
SapphireSun Avatar asked Sep 20 '10 18:09

SapphireSun


People also ask

How do you match letters in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .

How do I allow only letters and numbers in regex?

In order to verify that the string only contains letters, numbers, underscores and dashes, we can use the following regex: "^[A-Za-z0-9_-]*$".

Is only letter regex code?

To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[A-Za-z]+$/) which allows only letters.

Which below regex is applicable for alphabets?

[A-Za-z] will match all the alphabets (both lowercase and uppercase).


1 Answers

Perhaps you mean /[[:alpha:]]/? See perlre for the discussion of POSIX character classes.

like image 174
zigdon Avatar answered Oct 16 '22 12:10

zigdon