Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: String.replace(regex, string) to remove content from XML

Tags:

java

regex

xml

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?

like image 228
TookTheRook Avatar asked Dec 28 '22 18:12

TookTheRook


1 Answers

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)

like image 139
Sean Patrick Floyd Avatar answered Feb 04 '23 06:02

Sean Patrick Floyd