In Dart, I would like to split a string using a regular expression and include the matching delimiters in the resulting list. So with the delimiter ., I want the string 123.456.789 to get split into [ 123, ., 456, ., 789 ].
In some languages, like C#, JavaScript, Python and Perl, according to https://stackoverflow.com/a/15668433, this can be done by simply including the delimiters in capturing parentheses. The behaviour seems to be documented at https://ecma-international.org/ecma-262/9.0/#sec-regexp.prototype-@@split.
This doesn't seem to work in Dart, however:
print("123.456.789".split(new RegExp(r"(\.)")));
yields exactly the same thing as without the parentheses. Is there a way to get split() to work like this in Dart? Otherwise I guess it will have to be an allMatches() implementation.
Edit: Putting ((?<=\.)|(?=\.)) for the regex apparently does the job for a single delimiter, with lookbehind and lookahead. I will actually have a bunch of delimiters, and I'm not sure about efficiency with this method. Can someone advise if it's fine? Legibility is certainly reduced: to allow delimiters . and ;, would one need
((?<=\.)|(?=\.)|(?<=;)(?=;))
or
((?<=\.|;)|(?=\.|;).
Testing
print("123.456.789;abc;.xyz.;ABC".split(new RegExp(r"((?<=\.|;)|(?=\.|;))")));
indicates that both work.
(?!^|$)\b.- it could be one of a bunch of expressions.., it'll split on word boundary locations123.456.789;abc;.xyz.;ABC?