943,654 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3343
  • Java RSS
Aug 20th, 2009
0

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

Expand Post »
JAVA Syntax (Toggle Plain Text)
  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);

Quote ...
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?
Similar Threads
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Aug 20th, 2009
1

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

String[] splitPath = line.split("\\\\");
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Aug 20th, 2009
0

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

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.
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Aug 20th, 2009
0

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

I suposse, in "\\\\" are three kinds of character, one original \ and two meta characters in order m2,m1,m2,orig.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Adding Articles to database
Next Thread in Java Forum Timeline: Please help me to complete coding this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC