user3111525 user3111525 - 1 month ago 8
Groovy Question

Groovy: splitting multi-line string

I have got a file that looks like this:

FOoo
/
Bar
/
Bar foo Bar / asda
/
Lorem ipsum
ipsum lorem;
/


I want to split the text by a regex
^/$
, meaning line beginning and ending with delimeter
/
. I have tried various variants including
text.split(/^\/$/)
but it does not work. What am I missing?

Answer Source

In Groovy regex (that actually uses Java regex library), you may use a Pattern.MULTILINE inline embedded flag (?m) that will make ^ match a start of the line and $ the end of the line (rather than a whole string):

Multiline mode can also be enabled via the embedded flag expression (?m).

Use

text.split(/(?m)^\/$/)
            ^^^^