Q: Split string and also get delimiter in array I want to split a string like this:
"Street:§§§§__inboundRow['Adress']['Street']__§§§§ und Postal: §§§§__inboundRow['Adress']['Postal']__§§§§ super"

My code in Groovy:
def parts = ret.split(/§§§§__inboundRow.+?__§§§§/)

So the array what I get is
["Street:", " und Postal: ", " super"]

But what I want is:
["Street:", "§§§§__inboundRow['Adress']['Street']__§§§§", " und Postal: ", "§§§§__inboundRow['Adress']['Postal']__§§§§", " super"]

How do I achieve this?

A: Try splitting on a positive lookahead.  Since you want to retain the delimiters you use to split, a lookaround is probably the way to go here.


var str = "String1:§§§§__inboundRow[Test]__§§§§andString2:§§§§__inboundRow[Test1]__§§§§";
console.log(str.split(/(?=§§§§__inboundRow\[Test\d*\]__§§§§|and)/));


I don't know what exact language you are using, but this should work anywhere you may split using regex, with lookahead (JavaScript certainly supports it).  The pattern use to split was:
(?=§§§§__inboundRow\[Test\d*\]__§§§§|and)

This says to split when we can assert that what follows is either the text §§§§__inboundRow[Test\d*]__§§§§ or and.
