如何拆分字符串,同时保留分隔符? [英] How to split a string, but also keep the delimiters?
问题描述
我有一个由一组不同分隔符分隔的多行字符串:
I have a multiline string which is delimited by a set of different delimiters:
(Text1)(DelimiterA)(Text2)(DelimiterC)(Text3)(DelimiterB)(Text4)
我可以使用 String.split
将这个字符串拆分成它的部分,但似乎我无法获得与分隔符正则表达式匹配的实际字符串.
I can split this string into its parts, using String.split
, but it seems that I can't get the actual string, which matched the delimiter regex.
换句话说,这就是我得到的:
In other words, this is what I get:
Text1
Text2
Text3
Text4
这就是我想要的
Text1
DelimiterA
Text2
DelimiterC
Text3
DelimiterB
Text4
是否有任何 JDK 方法可以使用分隔符正则表达式拆分字符串但同时保留分隔符?
Is there any JDK way to split the string using a delimiter regex but also keep the delimiters?
推荐答案
可以使用lookahead和lookbehind,它们是正则表达式的特性.
You can use lookahead and lookbehind, which are features of regular expressions.
System.out.println(Arrays.toString("a;b;c;d".split("(?<=;)")));
System.out.println(Arrays.toString("a;b;c;d".split("(?=;)")));
System.out.println(Arrays.toString("a;b;c;d".split("((?<=;)|(?=;))")));
你会得到:
[a;, b;, c;, d]
[a, ;b, ;c, ;d]
[a, ;, b, ;, c, ;, d]
最后一个是你想要的.
((?<=;)|(?=;))
等于在之前选择一个空字符;
或;
.
Fabian Steeg 对可读性的评论是有效的.可读性始终是正则表达式的问题.为了使正则表达式更具可读性,我所做的一件事是创建一个变量,其名称表示正则表达式的作用.您甚至可以放置占位符(例如 %1$s
)并使用 Java 的 String.format
将占位符替换为您需要使用的实际字符串;例如:
Fabian Steeg's comments on readability is valid. Readability is always a problem with regular expressions. One thing I do to make regular expressions more readable is to create a variable, the name of which represents what the regular expression does. You can even put placeholders (e.g. %1$s
) and use Java's String.format
to replace the placeholders with the actual string you need to use; for example:
static public final String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))";
public void someMethod() {
final String[] aEach = "a;b;c;d".split(String.format(WITH_DELIMITER, ";"));
...
}
这篇关于如何拆分字符串,同时保留分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!