Parsing XML with regular expressions is fraught with problems and best avoided.
Instead, consider using XMLTask. It's an Ant task dedicated to parsing and modifying XML.
Download xmltask.jar and put it in the Ant lib
directory.
Below is part of an Ant script. It identifies <minorNumber>
elements that are siblings of <namespace>
elements where the <namespace>
elements have a text node starting with the string prefix3__
. It then replaces the text node under the <minorNumber>
element:
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" />
<xmltask dest="${dest.xml-file}">
<fileset file="src.xml"/>
<replace
path="//:packageVersions[starts-with(:namespace/text(), 'prefix3__')]/:minorNumber/text()"
withText="${correctMinorNumber}"
/>
</xmltask>
This script assumes that the XML is in a file named src.xml
and a property named dest.xml-file
points to where the modified of XML should go.