9

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!"]
Tjeu Foolen
  • 300
  • 2
  • 10

2 Answers2

9

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!
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • 1
    This is exactly what I was looking for! I knew it would be possible but couldn't see the solution. I also didn't know about the Pattern#compile, so another thing learned today! Thank you very much! – Tjeu Foolen May 15 '19 at 21:06
1

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!]

Ajinkyad
  • 37
  • 5