Alejandro59
11 months agoOccasional Contributor
String.replaceAll not working in Groovy script
I am using a file as a template in order to create new files. The template file contains "placeholders" in the form of [plc]placeholderName[plc]. I want to replace the placeholder at runtime with val...
- 11 months ago
This is best case to use Template Engine feature of groovy.
Instead of [plc]pattern[plc] => use $pattern or $variable
In your case, variables are originalSender, finalRecipient, AP_Initiator etc
Now the script will just look like below (only replacement part)
import groovy.text.SimpleTemplateEngine import java.time.* //Here define what value needs to be replaced for each variable def binding = [originalSender: '9928:12341234A', finalRecipient: '9928:12341234B', AP_Initiator: 'POP000106', TimeStampNormalized: System.currentTimeMillis(), TimeStampUTC: Instant.now().atZone(ZoneId.of("UTC"))] def template = new SimpleTemplateEngine().createTemplate(new File('C:/Templates/Template.xml').getText('UTF-8')).make(binding) log.info "Updated xml : ${template.toString()}"
By the way, you can quickly run the script here to see output.
Also wanted to point out why your script is not working.
[plc] - here it will find only p / l / c, but not plc
[, ] has to be escaped i.e., \[plc\]
Then \ has to be escaped again - so finally it will be '\\[plc\\]pattern\\[plc\\]'
Check here how it is done.