My basic intention is to find out whether a dir say A is immediately inside dir B or not. I'm checking it in java so I'm using Pattern and Matcher classes.
let say homeDir = /aa/bb/cc (home dir) dirB = /aa/bb/cc/dd/ee (not immediately inside home dir) dirC = /aa/bb/cc/dd (immediately inside home dir) dirD = /aa/ff (outside home dir)
I want the output to be true if the dir is immediately inside homeDir, otherwise false. My approach is I'm checking if homeDir path is matching with the given dir path. If it matches, I'm checking whether there are any / after the homeDir part. This is my sample code
private static final String REGEX =
"homeDir"+"/"+"((.+)(/?)(.+))";
private static final String INPUT =
"/aa/bb/cc/dd/ee";
public static void main(String[] args) {
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // get a matcher object
boolean output = false;
while(m.find()) {
System.out.println("Total : "+m.group(1)+"First : "+m.group(2)+ "Slash : "+m.group(3)+"Last : "+m.group(4));
if(m.group(3).length()==0)
{// that means match found and immediately inside also
output = true;
}
}
The out put I get is Total : dd/eeFirst : dd/eSlash : Last : e. All I wanted is I want the slash to be captured if there exists any. I think first .+ is greedy and taking all. I tried with possessive quantifier (/?+) but it throwing Exception in thread "main" java.util.regex.PatternSyntaxException: At position 3 in regular expression pattern: attempted to repeat a token that is already repeated /aa/bb/cc/((.+)(/?+)(.+)) ^ Please help me hot to go about this problem