Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matching letter characters [duplicate]

I've this regex:

if (cadena.matches("^[a-zA-Z ]+$")) return true;

It's accepting from A to Z as lowercase and uppercase. Also accepting spaces.

But this is working just for english. For instance, in Catalan we've the 'ç' character. Also we've characters with 'á', or 'à', etc.

Did some google and I couldn't find any way to do this.

I found out that I can filter for UTF-8 but this would accept characters that are not really a letter.

How can I implement this?

like image 953
Reinherd Avatar asked Jun 07 '13 09:06

Reinherd


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do I match a character in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

How do you repeat in regex?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

What is \r and \n in regex?

\n. Matches a newline character. \r. Matches a carriage return character.


1 Answers

Use this regex:

[\p{L}\s]+

\p{L} means any Unicode letter.

fiddle.re Demo.

like image 99
mvp Avatar answered Sep 30 '22 19:09

mvp