String line = "Extracting from E:\\Junk\\morejunk\\file.txt";
	System.out.println(line);	//prints:    Extracting from E:\Junk\morejunk\file.txt
		
	String[] splitPath = line.split("\\");	//exception thrown here
	String folder = splitPath[splitPath.length-2];
		
	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?

Recommended Answers

All 3 Replies

String[] splitPath = line.split("\\\\");

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.

I suposse, in "\\\\" are three kinds of character, one original \ and two meta characters in order m2,m1,m2,orig.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.