如何拆分字符串,还要保留分隔符? [英] How to split a string, but also keep the delimiters?

查看:1088
本文介绍了如何拆分字符串,还要保留分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多行字符串,由一组不同的分隔符分隔:

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
  • Text2
  • Text3
  • Text4

这是什么我想要


  • Text1

  • DelimiterA

  • Text2

  • DelimiterC

  • Text3

  • DelimiterB

  • Text4

  • Text1
  • DelimiterA
  • Text2
  • DelimiterC
  • Text3
  • DelimiterB
  • Text4

是否有任何JDK方法可以使用分隔符正则表达式来拆分字符串,但也保留了分隔符rs?

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. Like this:

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]

最后一个是你想要的。

((?< =;)|(?=;))等于在; 之前或<$ c之后选择一个空字符$ c>; 。

希望这会有所帮助。

编辑 Fabian Steeg对可读性的评论是有效的。可读性始终是RegEx的问题。有一件事,我做的是帮助缓解这个问题是创建一个变量,其名称代表正则表达式的作用,并使用Java String格式来帮助实现。像这样:

EDIT Fabian Steeg comments on Readability is valid. Readability is always the problem for RegEx. One thing, I do to help easing this is to create a variable whose name represent what the regex does and use Java String format to help that. Like this:

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

这有助于一点点。 :-D

This helps a little bit. :-D

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆