Lets say I have an XML in the form of a string. I wish to remove the content between two tags within the XML String, say . I have tried:
String newString = oldString.replaceFirst("\\<tagName>.*?\\<//tagName>",
                                                              "Content Removed");
but it does not work. Any pointers as to what am I doing wrong?
OK, apart from the obvious answer (don't parse XML with regex), maybe we can fix this:
String newString = oldString.replaceFirst("(?s)<tagName[^>]*>.*?</tagName>",
                                          "Content Removed");
Explanation:
(?s)             # turn single-line mode on (otherwise '.' won't match '\n')
<tagName         # remove unnecessary (and perhaps erroneous) escapes
[^>]*            # allow optional attributes
>.*?</tagName>   
Are you sure your matching the tag case correctly? Perhaps you also want to add the i flag to the pattern: (?si)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With