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!