简体   繁体   English

如何通过换行符和固定数量的制表符(如 Java 中的“\\n\\t”)拆分字符串?

[英]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,子目录1,
  • subdir2\\n\\t\\tfile.ext subdir2\\n\\t\\tfile.ext

The requirement is to split the input by "\\n\\t" but not "\\n\\t\\t".要求是按“\\n\\t”而不是“\\n\\t\\t”分割输入。 A simple try of一个简单的尝试

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

also splits "\\tfile.ext" from the last entry.还从最后一个条目拆分“\\tfile.ext”。 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:如果您的格式对于所有可能的输入(dir\\n\\tdir2\\n\\tdir3\\n\\t\\tfile.something)都是静态的,那么一种方法是:

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替换包含 \\n\\t 的每个 \\n\\t\\t
  2. Replace each \\n\\t替换每个\\n\\t
  3. Change back the \\n\\t\\t as you seemingly want to preserve it改回 \\n\\t\\t,因为您似乎想保留它
  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.这种方法更有效,因为它只使用 2 次拆分,但只有在末尾只有一个以 \\n\\t\\t 为前缀的元素时才有效。 Accessing an Array is kind of cheap O(1) so constant time.访问一个数组是一种廉价的 O(1) 所以恒定的时间。 More code but less full iterations (replaceAll, split).更多的代码但更少的完整迭代(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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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