r/javahelp • u/Vivid_Fly_3634 • May 01 '26
Solved How do I overcome this Escape Literal problem.
public class Main
{
public static String removeNonAlphanumeric(String input) {
if (input == null) {
return null;
}
return input.replaceAll("[^a-zA-Z0-9]", "");
}
public static void main(String[] args) {
System.out.println(removeNonAlphanumeric("How do we [']/['\.]['extend a face of an object.... "));
}
}Error I am getting:
Main.java:18: error: illegal escape character
System.out.println(removeNonAlphanumeric("How do we [']/['\.]['extend a face of an object.... "));
^
1 error
Here is the problem: Hi All, Seems like I am missing something very basic here. My purpose for this function is supposed to be to remove all non-alphanumeric characters from the string.
The \ in the string seems to cause problem since escape literals start with \
Why I cant put \\? : I need to use this in an application and user's input is not in my hands.