我试图在groovy中替换一个大字符串。但无法让它发挥作用。我正在使用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"
这会打印出原始变量
如果我更换一个较短的字符串,它会正确替换它。
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"
这会打印出预期的结果
答案 0 :(得分:2)
尝试这种模式:
http:\/\/10.33.0.69:8001\/VS_SiteFacilityLookup\/SiteFacilityLookupService\?XSD=\/com\/enbridge\/csim\/ebo\/module\/common\/serviceinterface\/SiteFacilityLookupService.xsd
你需要记住逃避特殊字符 - 例如?
- > \?
所以,总结一下,它结束为:
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"
我在这里测试了它:http://gskinner.com/RegExr/
关于主题:在用任何其他语言替换字符串,groovy,Java和(我希望!)时,字符串长度无关紧要。重要的是,在更大的模式中,更容易省略会导致不匹配的内容。所以你应该使用简单易懂的代码阅读器模式。
例如:
http:\/\/.*
- 匹配以http://