arrays - Split a string in Java, but keep the delimiters inside each new string


Keywords:java 


Question: 

I can't seem to find an answer for this one. What I want to do is to split a string in Java, but I want to keep the delimiters inside each string. For example, if I had the following string:

word1{word2}[word3](word4)"word5"'word6'

The array of new strings would have to be something like this:

["word1", "{word2}", "[word3]", "(word4)", "\"word5\"", "\'word6\'"]

How can I achieve this throughout Regex or other form? I'm still learning Regex in Java, so I tried some things, as discussed in here for example: How to split a string, but also keep the delimiters?

but I'm not getting the results I expect.

I have this delimiter:

static public final String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))";

And then this method:

private String[] splitLine() { return tokenFactor.split(String.format(WITH_DELIMITER, "\\(|\\)|\\[|\\]|\\{|\\}|\"|\'")); }

But that code splits the delimiters as individual strings, which is not what I want

Can anyone please help me?!! Thanks!


1 Answer: 

A solution using Pattern and regex :

I will catch every word alone, or words with one element before and after the String

String str = "word1{word2}[word3](word4)\"word5\"'word6'";
Matcher m = Pattern.compile("(([{\\[(\"']\\w+[}\\])\"'])|(\\w+))").matcher(str);
List<String> matches = new ArrayList<>();

while (m.find())
    matches.add(m.group());

String[] matchesArray = matches.toArray(new String[0]);
System.out.println(Arrays.toString(matchesArray));

I gave the way to have it in the an array, bu you can stop with the list

Regex demo