0

I will like to replace from:

"stable_dev/201904_xx/text1/text2.zip"
"stable_dev/201904/text5/text6.war"

into:

"stable_dev/new_value/text1/text2.zip"
"stable_dev/new_value/text5/text6.war"
I tried with

arrayList.toString().replaceAll("stable_dev/"+"[0-9a-zA-Z]*"+"[^a-zA-Z0-9]*"+"[0-9a-zA-Z]*"+"/", "stable_dev/new_value/"))

2 Answers2

0

This is only specific to your examples. Idea is to search for stable_dev/ and then locate everything till the next / Then replace that with the new value.

def str = "stable_dev/201904_xx/text1/text2.zip"

println str.replaceAll(/stable_dev\/.*?\//,"stable_dev/new_value/")​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

//Output:
//stable_dev/new_value/text1/text2.zip​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
Kaus2b
  • 747
  • 4
  • 12
-1

I'd use split to avoid over-complicated regexp:

def replacer = { String replacement, String val ->
  def parts = val.split( /\d{6}_?([a-z0-9]{2})?/ )
  2 != parts.size() ? val : "${parts[ 0 ]}$replacement${parts[ 1 ]}"
}

def results = [
  'aaa',
  "stable_dev/201904_xx/text1/text2.zip",
  "stable_dev/201904/text5/text6.war"
].collect replacer.curry( 'new_value' )

assert results == [ 'aaa', 'stable_dev/new_value/text1/text2.zip', 'stable_dev/new_value/text5/text6.war' ]
injecteer
  • 20,038
  • 4
  • 45
  • 89