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:
文本1
Text2
Text3
Text4
这就是我想要的
文本1
分隔符A
Text2
分隔符C
Text3
分隔符B
Text4
是否有任何 JDK 方法可以使用分隔符正则表达式拆分字符串但同时保留分隔符?
Is there any JDK way to split the string using a delimiter regex but also keep the delimiters?
推荐答案
可以使用正则表达式的前瞻和后瞻功能.
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]
最后一个就是你想要的.
The last one is what you want.
((?<=;)|(?=;))
等于选择;
之前或;
之后的空字符.
((?<=;)|(?=;))
equals to select an empty character before ;
or after ;
.
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, ";"));
...
}
这篇关于如何拆分字符串,但还要保留分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何拆分字符串,但还要保留分隔符?
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01