0

I have a groovy script that you can find in this link.

String str = '# hosts.inventory [vpc] vpc ec2  [ec2] vpc ec2  [project:children] vpc ec2  [author]   [publisher]   [dispatcher] '
str.split((/^\[author\]$/))
for( String values : str )     
println(values);

I would like to split my string into two parts:

  1. Everything before [author] ie # hosts.inventory [vpc] vpc ec2 [ec2] vpc ec2 [project:children] vpc ec2
  2. Everything after [author] ie [publisher] [dispatcher]

My regex doesn't seem to work. How do I solve this problem?

Emma
  • 1
  • 9
  • 28
  • 53
Archit Arora
  • 1,826
  • 6
  • 29
  • 57

1 Answers1

1

This might simply work:

String str = '# hosts.inventory [vpc] vpc ec2  [ec2] vpc ec2  [project:children] vpc ec2  [author]   [publisher]   [dispatcher] '
values = str.split((/(\[author\])/))
println(values);

Groovy


Or I'm guessing that the expression you might be looking for would be similar to:

(.+)(\[author\])(.+)

Where there are three capturing groups, two for before and after [author]:

(.+)

and one for the author:

(\[author\])

Demo

Emma
  • 1
  • 9
  • 28
  • 53