Neel Neel - 11 months ago 74
Java Question

How to escape a carat in Regex expression in Java

I have a regex expression which removes all non alphanumeric characters. It is working fine for all special characters apart from ^. Below is the regex expression I am using.

String strRefernce = strReference.replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]", "").toUpperCase();

I tried modifying it to

String strRefernce = strReference.replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}]\\^", "").toUpperCase();


String strRefernce = strReference.replaceAll("[^\\p{IsAlphabetic}^\\p{IsDigit}\\^]", "").toUpperCase();

But these are also not able to remove this symbol.
Can someone please help me with this.


The first ^ inside [^...] is a negation mark making the character class a negated one (matching characters other than what is inside).

The second one inside is considered a literal - thus, it should not be matched with the regex. Remove it, and a caret will get matched with it:


or even shorter:


The \P{Alnum} class stands for any character other than an alphanumeric character: [\p{Alpha}\p{Digit}] (see Java regex reference). When you pass (?U), the \P{Alnum} class will not match Unicode letters. See this IDEONE demo.

Add a + at the end if you want to remove whole chunks of symbols other than \\p{IsAlphabetic} and \\p{IsDigit}.

enter image description here