如何拆分字符串,但还要保留分隔符?

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, ";"));
    ...
}

这篇关于如何拆分字符串,但还要保留分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何拆分字符串,但还要保留分隔符?

基础教程推荐