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?
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).
\p{L} matches a single code point in the category "letter". \p{N} matches any kind of numeric character in any script.
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).
[A-Za-z] will match all the alphabets (both lowercase and uppercase).
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.
It is possible, but it is Java specific:
[\p{L}&&[^\p{Alpha}]]
(quote as appropriate in a Java string etc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With