Search code examples
regexsearchantreplaceapostrophe

Ant with apostrophe replace


I want to replace

from expression="ora:processXSLT('Transformation_1.xsl',bpws:getVariableData('inputVariable','payload'))

to

from expression="ora:processXSLT('xsd/Transformation_1.xsl',bpws:getVariableData('inputVariable','payload'))

Could any one suggest to me pattern a to choose?

I have tried:

<replaceregexp  flags="g">
    <regexp pattern="ora:processXSLT("/>
        <substitution expression="ora:processXSLT('xsd\1)"/> 
    <fileset dir="${fmw.prop}" includes="*"/>
</replaceregexp>

But it is not working


Solution

  • <replaceregexp  flags="g">
        <regexp pattern="([^']+')(.*)"/>
            <substitution expression="\1xsd/\2)"/> 
        <fileset dir="${fmw.prop}" includes="*"/>
    </replaceregexp>
    

    In your regex, you are misusing \1. When you write a regex, everything you place between parenthesis is captured and then can be reinjected with \1. If there is more parenthesis groups, their content can be reinjected too using \2, \3, and so on. That means that when you want to match a real parenthesis, e.g. the ( in ora:processXSLT(, you need to escape it like so: \(. That way it does not become a capturing parenthesis but is integrated into the matching pattern.

    Furthermore, you need to capture and reinject everything you want to have in the final substitution, so you need to match the entire string, make your modifications, and return the newly formed string. That is why we are matching ora:processXSLT(' with [^']+', matching Transformation_1.xsl',bpws:getVariableData('inputVariable','payload')) with (.*), and placing xsd/ in between both strings to obtain the proper result.