Home > Blockchain >  StringUtils replace text in between two patterns
StringUtils replace text in between two patterns

Time:10-30

Hi I found really useful the apache operator

StringUtils.substringBetween(fileContent, "<![CDATA[", "]]>") 

to extract information inside

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<envelope>
    <xxxx>
        <yyyy>
            <![CDATA[

                    <?xml version="1.0" encoding="UTF-8" ?>
                    <Document >
                        <eee>
                            <tt>
                                <ss>zzzzzzz</ss>
                                <aa>2021-09-09T10:39:29.850Z</aa>
                                <aaaa>
                                    <Cd>cccc</Cd>
                                </aaaa>
                                <dd>ssss</dd>
                                <ff></ff>
                            </tt>
                        </eee>
                    </Document>
                ]]>
        </yyyy>
    </xxxx>
</envelope>

But now what I'm looking is another operator or regex that allow me to replace a dynamic xml

![CDATA["old_xml"]] 

by another xml

![CDATA["new_xml"]]

Any idea idea how to accomplish this?

Regards.

CodePudding user response:

You can use the regex, (\<!\[CDATA\[).*?(\]\]>).

Demo:

public class Main {
    public static void main(String[] args) {
        String xml = """
                ...
                    <data><![CDATA[a < b]]></data>
                ...
                """;

        String replacement = "foo";

        xml = xml.replaceAll("(\\<!\\[CDATA\\[).*?(\\]\\]>)", "$1"   replacement   "$2");

        System.out.println(xml);
    }
}

Output:

...
    <data><![CDATA[foo]]></data>
...

Explanation of the regex:

  • ( : Start of group#1
    • \<!\[CDATA\[ : String <![CDATA[
  • ) : End of group#1
  • .*? : Any character any number of times
  • ( : Start of group#2
    • \]\]>: String ]]>
  • ) : End of group#2

CodePudding user response:

Instead of StringUtils, you can use String#replaceAll method:

fileContent = fileContent
  .replaceAll("(?s)(<!\\[CDATA\\[). ?(]]>)", "$1foo$2");

Explanation:

  • (<!\\[CDATA\\[): Match opening <![CDATA[ substring and capture in group #1
  • . ?: Match 0 or more of any characters including line break
  • (]]>): Match closing ]]? substring and capture in group #2
  • $1foo$2: Replace with foo surrounded with back-references of capture group 1 and 2 on both sides
  • Related