Since all sections are separated by line separator \n
you can simply use .+
to match them since by default dot .
can't match line separators, so you are sure that it will stop before \n
(or any other line separator like \r
).
You can also add some groups to separate tabs from actual segment like named group (?<tabs>\t*)
to match zero or more tabs at start of each match.
To print rest of text after match simply substring after index of last matched character (you can obtain it via Matcher#end
).
To print string which will contain \n
and \t
(not as literals but as pair of backslash and letter) you can either manually replace each "\n"
with "\\n"
and "\t"
with "\\t"
or use utility class like StringEscapeUtils
from org.apache.commons.lang
which contains escapeJava
method which does it for us.
So your code can look like:
String path = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
Pattern p = Pattern.compile("(?<tabs>\t*)(?<segment>.+)");//dot can't match line separators
Matcher m = p.matcher(path);
int i = 1;
while(m.find()){
System.out.println("iteration: " + i++);
System.out.println("Preceding tabs: " + (m.group("tabs").length()));
System.out.println("Segment: " + m.group("segment"));
System.out.println("Rest: "+ StringEscapeUtils.escapeJava(path.substring(m.end())));
System.out.println();
}
Output:
iteration: 1
Preceding tabs: 0
Segment: dir
Rest: \n\tsubdir1\n\tsubdir2\n\t\tfile.ext
iteration: 2
Preceding tabs: 1
Segment: subdir1
Rest: \n\tsubdir2\n\t\tfile.ext
iteration: 3
Preceding tabs: 1
Segment: subdir2
Rest: \n\t\tfile.ext
iteration: 4
Preceding tabs: 2
Segment: file.ext
Rest: