Reputation: 13
I am running below 2 lines in Java (Java 8):
String dirTree = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
String[] result = dirTree.split("\\n\\t[^\\t]");
Result seen - result:
["dir", "ubdir1", "ubdir2\n\t\tfile.ext"]
I was expecting - result:
["dir", "subdir1", "subdir2\n\t\tfile.ext"]
Can someone please explain why the first character of the strings in result are missing(Eg - "ubdir1" instead of "subdir1") ?
Upvotes: 1
Views: 78
Reputation: 10466
Just Split by this :
\n\t(?!\t)
Explanation:
\n\t
matches one newline and one tab(?!\t)
negative lookahead to ensure no \t followed immediately
after \n\t
So the difference between (?!\t)
and [^\t]
is that the first one returns true or false and second one matches the character. So in your case, it matched the non tab character and used that to split as well.
Upvotes: 1
Reputation: 114
You have to understand how the regex pattern works before applying it.
Your regex pattern is \n\t[^\t]
This search for a pattern with \n\t
and any character except \t
. [^\t]
negates the pattern. This pattern matches any character except \t
. So in your case it is matching s
because its is any character except \t
.
To get your expected result your pattern should be \n\t
Upvotes: 0