Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression match all \p{L} but not \p{Alpha}

How can I match all \p{L} but not \p{Alpha} in a regular expression? Is it possible to implement a logical AND in Java's Regexp? If the answer is yes, how can that be achieved?

like image 309
Minh Le Avatar asked Feb 21 '14 07:02

Minh Le


People also ask

How do you match letters 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).

What is \p l in regex?

\p{L} matches a single code point in the category "letter". \p{N} matches any kind of numeric character in any script.

What's the difference between () and [] in regular expression?

Additionally the - character has special meaning inside of a [] . It provides a range construct. The regex [a-z] will match any letter a through z. The () construct is a grouping construct establishing a precedence order (it also has impact on accessing matched substrings but that's a bit more of an advanced topic).

Which below regex is applicable for alphabets?

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


2 Answers

Yes, by using a negated character class:

[^\P{L}\p{Alpha}]

[^\P{L}] matches the same as \p{L}, but the negated character class makes it possible to subtract characters/properties from that set of characters.

like image 199
Tim Pietzcker Avatar answered Oct 04 '22 08:10

Tim Pietzcker


It is possible, but it is Java specific:

[\p{L}&&[^\p{Alpha}]]

(quote as appropriate in a Java string etc)

like image 29
fge Avatar answered Oct 01 '22 08:10

fge