简体   繁体   中英

How to split a string by a newline and a fixed number of tabs like "\n\t" in Java?

My input string is the following:

 String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";

My intended result is

  • dir,
  • subdir1,
  • subdir2\\n\\t\\tfile.ext

The requirement is to split the input by "\\n\\t" but not "\\n\\t\\t". A simple try of

String[] answers = input.split("\n\t");

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

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

\n\t(?!\t)

See a regex demo .

String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
String[] answers = input.split("\\n\\t(?!\\t)");
System.out.println(Arrays.toString(answers));

Output

[dir, subdir1, subdir2
        file.ext]

If you are looking for a generic approach, it highly depends on what format will input generally have. If your format is static for all possible inputs (dir\\n\\tdir2\\n\\tdir3\\n\\t\\tfile.something) one way to do it is the following:

String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
String[] answers = input.split("\n\t");

for (int i = 1; i < answers.length; i++)
    if (answers[i].contains("\t"))
        answers[i-1] = answers[i-1] + "\n\t" + answers[i];

String[] answersFinal = Arrays.copyOf(answers, answers.length-1);
for (int i = 0; i < answersFinal.length; i++)
    answersFinal[i] = answers[i];

for (String s : answersFinal)
    System.out.println(s);

However this is not a good solution and I would suggest reformatting your input to include a special sequence of characters that you can use to split the input, for example:

String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
input = input.replaceAll("\n\t", "%%%").replaceAll("%%%\t", "\n\t\t");

And then split the input with '%%%', you will get your desired output.

But again, this highly depends on how generic you want it to be, the best solution is to use an overall different approach to achieve what you want, but I cannot provide it since I don't have enough information on what you are developing.

You can simply do:

String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
String[] modifiedInput = input.replaceAll("\n\t\t", "####").replaceAll("\n\t", "§§§§").replaceAll("####", "\n\t\t").split("§§§§");
  1. Replace each \\n\\t\\t which contain the \\n\\t
  2. Replace each \\n\\t
  3. Change back the \\n\\t\\t as you seemingly want to preserve it
  4. Make the split.

Not very efficient but still works fast enough if you won't use it in mass data situations.

This approach is more efficient as it only uses 2 splits but only works if there is only one element prefixed with \\n\\t\\t at the end. Accessing an Array is kind of cheap O(1) so constant time. More code but less full iterations (replaceAll, split).

final String input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";
final String[] s1 = input.split("\n\t\t");
final String last  = s1[s1.length - 1];
final String[] modifiedInput = s1[0].split("\n\t");
modifiedInput[modifiedInput.length -1] = modifiedInput[modifiedInput.length -1] + "\n\t\t" + last;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM