mightguy
mightguy

Reputation: 175

its possible replace with variable in groovy (java regex )?

for example replace with varible group replacement

 def x=2

  "var1var2".replaceAll(/var(.*?)var(.+)/ , '$${x}' )

for obtain 2

Upvotes: 0

Views: 619

Answers (1)

tim_yates
tim_yates

Reputation: 171084

I think you want:

"var1var2".replaceAll(/var(.*?)var(.+)/ , "\$${x}")

That will return the second group, so 2

A better example (with only one 2 in it would be)

"var3var4".replaceAll(/var(.*?)var(.+)/ , "\$${x}")

That will return 4

Upvotes: 1

Related Questions