How to split string but keep delimiters in java? [duplicate]

泪湿孤枕 提交于 2021-01-22 05:25:10

问题


I'm trying to recreate the way discord parses messages with emoji's inside of it.

For example, I want the message Hello, :smile::hearth: world! to split into the following array:

["Hello, ", ":smile:", ":hearth:", " world!"]

I've already tried to split the array with the following code:

Arrays.toString(message.split("(:[A-Za-z]+:)"))

However, the split method removes the delimiters found. So the end result looks like this:

["Hello", , , " world!"]

回答1:


As from your input string and expected results, I can infer that you want to split your string basically from three rules.

  • Split from the point which is preceded and followed by a colon
  • Split from the point which is preceded by a space and followed by a colon
  • Split from the point which is preceded by a colon and followed by a space

Hence you can use this regex using alternations for all three cases mentioned above.

(?<=:)(?=:)|(?<= )(?=:)|(?<=:)(?= )

Regex Demo

Java code,

String s = "Hello, :smile::hearth: world!";
System.out.println(Arrays.toString(s.split("(?<=:)(?=:)|(?<= )(?=:)|(?<=:)(?= )")));

Prints like your expected output,

[Hello, , :smile:, :hearth:,  world!]

Also, as an alternative if you can use matching the text rather than split, the regex would be much simpler to use and it would be this,

:[^:]+:|\S+

Regex Demo using match

Java code,

String s = "Hello, :smile::hearth: world!";
Pattern p = Pattern.compile(":[^:]+:|\\S+");
Matcher m = p.matcher(s);
while(m.find()) {
    System.out.println(m.group());
}

Prints,

Hello,
:smile:
:hearth:
world!



回答2:


Please use regular expression's Lookahead ,Lookbehind to get expected result. Please refer below code snippet to

 public static void main(String[] args) {
       String message= "Hello, :smile::hearth: world!"; 
       System.out.println(Arrays.toString(message.split("(?=,)|(?=(?!)::)|(?<=(:[A-Za-z]+:))")));


    }

Which will give output as [Hello, , :smile:, :hearth:, world!]



来源:https://stackoverflow.com/questions/56156363/how-to-split-string-but-keep-delimiters-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!