String pattern throws java.util.regex.PatternSyntaxException

In an attempt to extract the depth of a folder in a platform oblivious way using strings containing the path, ImageJ1 throws me a Java exception if I use the function below. Is there a built-in way to count the number of backslashes in a Windows file path string?

This is a minimal example:

function countOf(haystack,needle){
	non = lengthOf(haystack)-lengthOf(bale);
	return non;

	haystack = "/Volumes/datafiles/this/is/a/test/";
	needle = "/";
	print ("count of " + needle + " is "+countOf(haystack,needle));
	haystack = "D:\\datafiles\this\\is\\a\\test\\";
	needle = "\\";
	print ("count of " + needle + " is "+countOf(haystack,needle));

If this is a true and existing path, I can use File.getParent(folder) but that does not work in a test environment where haystack is a string, not a real path. So also this function doesn’t help me out:

function depthOf(folder){
	if(folder == ""){
		return 0;
		return 1 + depthOf(File.getParent(folder));

Problem is in the replace() function. Using the following recursive function does work.

	haystack = "d:\\datafiles\\this\\is\\a\\test";
	needle = "\\";
	print ("count of " + needle + " is "+countOfRec(haystack,needle));

function countOfRec(haystack,needle){
	if(lengthOf(haystack)==0 || lof == -1) return 0;	
	return ss;
1 Like