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, ";"));
...
}
这篇关于如何拆分字符串,但还要保留分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何拆分字符串,但还要保留分隔符?
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01