I have a multiline string which is delimited by a set of different delimiters:
(Text1)(DelimiterA)(Text2)(DelimiterC)(Text3)(DelimiterB)(Text4)
I can split this string into its parts, using String.split, but it seems that I can’t get the actual string, which matched the delimiter regex.
In other words, this is what I get:
Text1Text2Text3Text4
This is what I want
Text1DelimiterAText2DelimiterCText3DelimiterBText4
Is there any JDK way to split the string using a delimiter regex but also keep the delimiters?
You can use lookahead and lookbehind, which are features of regular expressions.
And you will get:
The last one is what you want.
((?<=;)|(?=;))equals to select an empty character before;or after;.EDIT: Fabian Steeg’s comments on readability is valid. Readability is always a problem with regular expressions. One thing I do to make regular expressions more readable is to create a variable, the name of which represents what the regular expression does. You can even put placeholders (e.g.
%1$s) and use Java’sString.formatto replace the placeholders with the actual string you need to use; for example: