scorpion05
scorpion05

Reputation: 37

Replacing a large String in groovy

I am trying to replace a large string in groovy. But can't get it to work. I am using groovy 1.8.6

def textn = "http://10.33.0.69:8001/VS_SiteFacilityLookup/SiteFacilityLookupService?XSD=/com/enbridge/csim/ebo/module/common/serviceinterface/SiteFacilityLookupService.xsd"
textn = textn.replaceAll("http://10.33.0.69:8001/VS_SiteFacilityLookup/SiteFacilityLookupService?XSD=/com/enbridge/csim/ebo/module/common/serviceinterface/SiteFacilityLookupService.xsd", "hola")
println "textn : $textn"

This prints out the original variable

If I replace a shorter string, it replaces it correctly.

def textn = "http://10.33.0.69:8001/VS_SiteFacilityLookup/SiteFacilityLookupService?XSD=/com/enbridge/csim/ebo/module/common/serviceinterface/SiteFacilityLookupService.xsd"
textn = textn.replaceAll("SiteFacilityLookupService.xsd", "hola")
println "textn : $textn"

This prints out the expected result

Upvotes: 1

Views: 568

Answers (1)

dantuch
dantuch

Reputation: 9293

try this pattern:

http:\/\/10.33.0.69:8001\/VS_SiteFacilityLookup\/SiteFacilityLookupService\?XSD=\/com\/enbridge\/csim\/ebo\/module\/common\/serviceinterface\/SiteFacilityLookupService.xsd

you need to remember to escape special characters - for e.g. ? -> \?

so, to sum up, it ends as:

def textn = "http://10.33.0.69:8001/VS_SiteFacilityLookup/SiteFacilityLookupService?XSD=/com/enbridge/csim/ebo/module/common/serviceinterface/SiteFacilityLookupService.xsd"
textn = textn.replaceAll("http:\/\/10.33.0.69:8001\/VS_SiteFacilityLookup\/SiteFacilityLookupService\?XSD=\/com\/enbridge\/csim\/ebo\/module\/common\/serviceinterface\/SiteFacilityLookupService.xsd", "hola")
println "textn : $textn"

I have tested it here: http://gskinner.com/RegExr/

On the topic: While replacing Strings, in groovy, Java, and (I hope!) in any other language, String length doesn't matter. What does matter is that in larger pattern it's easier to omit something that will result in NO match. So you sould be using patterns that are simple, and easy to understand by the reader of code.

for example:

http:\/\/.* - matches every String that starts with http://

Upvotes: 2

Related Questions