String.split("\\"); throws PatternSyntaxException?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

String.split("\\"); throws PatternSyntaxException?

 
0
  #1
Aug 20th, 2009
  1. String line = "Extracting from E:\\Junk\\morejunk\\file.txt";
  2. System.out.println(line); //prints: Extracting from E:\Junk\morejunk\file.txt
  3.  
  4. String[] splitPath = line.split("\\"); //exception thrown here
  5. String folder = splitPath[splitPath.length-2];
  6.  
  7. System.out.println(folder);

Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
My objective is to always print the folder containing the file being extracted (morejunk in this case).

Any suggestions why it wont accept "\\" as a pattern which is correctly syntaxed?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: String.split("\\"); throws PatternSyntaxException?

 
1
  #2
Aug 20th, 2009
String[] splitPath = line.split("\\\\");
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

Re: String.split("\\"); throws PatternSyntaxException?

 
0
  #3
Aug 20th, 2009
hmm... well thats funny..

You are correct.. Here I specified the cmd with 2 \'s, but ur way also works when line is given as a paramater, using only 1 \'s.

I guess java automatically throws in another \ on its own every time it sees only 1 \ so \\\\ works.

Thank you.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: String.split("\\"); throws PatternSyntaxException?

 
0
  #4
Aug 20th, 2009
I suposse, in "\\\\" are three kinds of character, one original \ and two meta characters in order m2,m1,m2,orig.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 482 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC