Jump to content

Replace '\' with '\\'

- - - - -

  • Please log in to reply
3 replies to this topic

#1
DavidO

DavidO

    Newbie

  • Members
  • PipPip
  • 14 posts
Right now I am working on a code parser project for a program that has thousands of files. I can parse the code of a list of files and I can recursively traverse through a folder in order to find all of the files. The problem I am running into is connecting the two.

   public void recursiveTraversal(File fileObject){		

        if (fileObject.isDirectory()){

            File allFiles[] = fileObject.listFiles();

            for(File aFile : allFiles){

                recursiveTraversal(aFile);

        }

        }else if (fileObject.isFile()){

            System.out.println(indent  + "  " + fileObject.getName());

            try {

            	String CanonicalPath = fileObject.getCanonicalPath();

            	String newCanonicalPath = CanonicalPath.replaceAll("\\", "\\\\");

            	System.out.println(newCanonicalPath);

			} catch (IOException e) {

				// TODO Auto-generated catch block

				e.printStackTrace();

			}

        }		

    }

String newCanonicalPath = CanonicalPath.replaceAll("\\", "\\\\");
I have also tried just using replace("\\","\\\\")

I am getting the Canonical path of the fileObject but when i try to replace '\' with '\\', in order to send it into the code parser which requires double backslashes (One of them is considered an escape character), I am running into errors.

[COLOR="red"] Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1

\

 ^

	at java.util.regex.Pattern.error(Pattern.java:1724)

	at java.util.regex.Pattern.compile(Pattern.java:1477)

	at java.util.regex.Pattern.<init>(Pattern.java:1144)

	at java.util.regex.Pattern.compile(Pattern.java:834)

	at java.lang.String.replaceAll(String.java:1573)

	at com.foldertraversal.FolderTraversal.recursiveTraversal(FolderTraversal.java:35)

	at com.foldertraversal.FolderTraversal.recursiveTraversal(FolderTraversal.java:29)

	at com.foldertraversal.FolderTraversal.recursiveTraversal(FolderTraversal.java:29)

	at com.foldertraversal.FolderTraversal.traverse(FolderTraversal.java:20)

	at com.foldertraversal.Main.main(Main.java:10)

[/COLOR]

I have tried a bunch of things but none of which seem to work so if anyone can figure it out I would be extremely grateful!
"Encapsulation is a little like Las Vegas. What happens in a class, stays in a class." - ArcaneCode
Languages: C/C++, Python, Java/JavaScript, Ruby, SQL , HTML5/CSS

#2
DavidO

DavidO

    Newbie

  • Members
  • PipPip
  • 14 posts
Well, I must say that you should not trust google... Originally I had replace("\","\\") then I read somewhere that I should use replaceAll......Long story short replace("\\","\\\\") works.
"Encapsulation is a little like Las Vegas. What happens in a class, stays in a class." - ArcaneCode
Languages: C/C++, Python, Java/JavaScript, Ruby, SQL , HTML5/CSS

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The reason that one works and your replaceAll didn't is replaceAll uses a regular expression as first parameter. To match 1 backslash with a regex in java you need 4.
replaceAll("\\\\","\\\\\\\\");
would've propably worked.

Quote

Why the f***, 4 backslashes?
Backslash in Java is, as you propably know, the escape character. Unfortunately it's also the escape character for the regular expression language.
So if you want a backslash in a regex, you escape it with another: \\
Now, because you're writing this in a .java file and it must go trough the compiler, you must escape every backslash again --> 4 total.

#4
DavidO

DavidO

    Newbie

  • Members
  • PipPip
  • 14 posts
The replace all you suggested did in fact work, Thanks for all the help!

or should i say...

010101000110100001100001011011100110101101110011001000000110011001101111011100100010000001100001011011000110110000100000011101000110100001100101001000000110100001100101011011000111000000100001
"Encapsulation is a little like Las Vegas. What happens in a class, stays in a class." - ArcaneCode
Languages: C/C++, Python, Java/JavaScript, Ruby, SQL , HTML5/CSS




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users