user3111525
user3111525

Reputation: 5203

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?

Upvotes: 1

Views: 2827

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

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)^\/$/)
            ^^^^

Upvotes: 2

Related Questions