I'm trying to split a string using a variety of characters as delimiters and also keep those delimiters in their own array index. For example say I want to split the string:
if (x>1) return x * fact(x-1);
using '(', '>', ')', '*', '-', ';' and '\s' as delimiters. I want the output to be the following string array: {"if", "(", "x", ">", "1", ")", "return", "x", "*", "fact", "(", "x", "-", "1", ")", ";"}
The regex I'm using so far is
split("(?=(\\w+(?=[\\s\\+\\-\\*/<(<=)>(>=)(==)(!=)=;,\\.\"\\(\\)\\[\\]\\{\\}])))")
which splits at each word character regardless of whether it is followed by one of the delimiters. For example
test + 1
outputs {"t","e","s","t+","1"} instead of {"test+", "1"}
Why does it split at each character even if that character is not followed by one of my delimiters? Also is a regex which does this even possible in Java? Thank you