googlethesavior
googlethesavior

Reputation: 13

Unable to understand the result of RegEx split for a string

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

Answers (2)

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10466

Just Split by this :

\n\t(?!\t)

Explanation:

  1. \n\t matches one newline and one tab
  2. (?!\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

Mars Moon
Mars Moon

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

Related Questions