Skip to content
Advertisement

How to split a string by a newline and a fixed number of tabs like “nt” in Java?

My input string is the following:

JavaScript

My intended result is

  • dir,
  • subdir1,
  • subdir2nttfile.ext

The requirement is to split the input by “nt” but not “ntt”. A simple try of

JavaScript

also splits “tfile.ext” from the last entry. Is there a simple regular expression to solve the problem? Thanks!

Advertisement

Answer

You can split on a newline and tab, and assert not a tab after it to the right.

JavaScript

See a regex demo.

JavaScript

Output

JavaScript
Advertisement