\ is used as for escape sequence in many programming languages, including Java.
If you want to
- go to next line then use
\nor\r, - for tab use
\t - likewise to print a
\or"which are special in string literal you have to escape it with another\which gives us\\and\"
\ is used as for escape sequence in many programming languages, including Java.
If you want to
- go to next line then use
\nor\r, - for tab use
\t - likewise to print a
\or"which are special in string literal you have to escape it with another\which gives us\\and\"
Imagine you are designing a programming language. You decide that Strings are enclosed in quotes ("Apple"). Then you hit your first snag: how to represent quotation marks since you've already used them ? Just out of convention you decide to use \" to represent quotation marks. Then you have a second problem: how to represent \ ? Again, out of convention you decide to use \\ instead. Thankfully, the process ends there and this is sufficient. You can also use what is called an escape sequence to represent other characters such as the carriage return (\n).
Videos
You only need the \\ when you hardcode it in a String literal. Because \ is the escape character, it becomes a single \ at runtime. You can also write your paths with / as a separator. To be clear
String path = "C:\\Users\\chloe\\Documents";
creates a String that has a value equal to C:\Users\chloe\Documents (if you were to print it). You could also write
String path = System.getProperty("user.home") + File.separator + "Documents";
which would then select the Documents folder of the user at run-time. Finally,
System.out.println("\\\\");
System.out.println("\\\\".replaceAll("(\\\\\\\\)+", "\\\\"));
Will output
\\
\
Escaping \ in a regular expression is counter intuitive.
path.replaceAll('\\',File.separator);
may work. More information may be helpful, such as how you are getting the path and what class it is (are you reading a File path to a path or a string, are you generating the path piece by piece).
You need to apply two levels of escaping - one for the regex itself, and one for a Java string literal. That means you need four backslashes in a row. So:
replaceAll("[^-\\dA-Za-zรรรข`~!@#$%^&*()_+=[{]}\\:;<,>.?/\\\\ ]", "")
I assume the \\d was meant to cover any digit, rather than actually putting d in the list?
You may find it easiest to print out your pattern to the console, so you can see exactly what the regex engine sees, without Java string literal escaping being relevant. The above pattern is:
[^-\dA-Za-zรรรข`~!@#$%^&*()_+=[{]}\:;<,>.?/\\ ]
So the bits with backslashes are:
\d(digit)\:(colon)\\(backslash)
To code a single literal backslash in regex, you need four backslashes (\\\\) in the code (see last char):
[^-\\dA-Za-zรรรข`~!@#$%^&*()_+=[{]}:;<,>.?/ \\\\]
Each pair of backslashes is a single backslash in the string, and you need two of those so it is further escaped in the regex; you need to escape the escape.
Will we ever escape this coding eyesore in java? (trying hard for the pun there)