如何拆分字符串,同时保留分隔符?

编程入门 行业动态 更新时间:2024-10-25 10:31:05
本文介绍了如何拆分字符串,同时保留分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

如何拆分字符串,同时保留分隔符?

本文发布于:2023-11-05 19:13:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   分隔符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!