lundi 9 octobre 2017

Does Java Actually Escape Back Slashes how it claims?

What I understand is that every backslash requires one more backslash to be escaped for regex. I have the following input String in Java:

C:\\my\\drive\\application\\webapps\\ROOT\\classes\\there\\is\\a\\clazz

to remove everything upto classes\\, I added the following regex in Java replaceAll() method:

.*\\\\b(classes)(\\\\W\\\\S|\\\\/){1,2}\\\\b

Why i have added four slashes

  1. One backslash escaped to indicate that this is regex \\

  2. One backslash escaped to indicate that the regex input is going as a java String \\.

But even with one set of doubled-backslash \\ it doesn't work.

When I tried it on regexr and regexplanet, it seems to be working correctly for both unix/windows type of filepaths and gave me what I wanted. It showed me the correct replaceAll() results as desired.

But when I put it into the code, it's not working at all.

So how many times in Java do we need to actually escape each backslash to get this to work?

Aucun commentaire:

Enregistrer un commentaire