简体   繁体   中英

Regex - Replace text between tags with new line characters

I have extracted the request text from a soaupui request and I'm trying to replace everything from with a new header. I have tried several different expressions but having trouble matching I think because the text contains new line characters.

Below is the latest one I have tried

strRequest3 = strRequest3.replaceAll("<soapenv:Envelope(.*?
\\R)<soapenv:Header/>","<soap:Envelope " + strNewHeader + "</soap:Header>");
log.info strRequest3

Sample XML below where I'm trying to insert a new first line and header

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:urn="urn:dhs-gov-au:ccs:pms:sessionreport">
<soapenv:Header/>
<soapenv:Body>

Any ideas on what regex I can try to achieve the above would be very appreciated

Thanks

If you want .* to match newlines you will have to use s flag (dotall) for regex.

In Java you can set this flag by adding (?s) in front of regex.

String str = "<soapenv:Envelope>123123\n" +
            "123123\n" +
            "1231422343\n<soapenv:Header/>";
String replacement = "new";

System.out.println(str.replaceAll("(?s)<soapenv:Envelope>(.*)<soapenv:Header/>",
            "<soapenv:Envelope>" + replacement + "<soapenv:Header/>"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM