I have a simple xml file and I want to remove everything before the first <item> tag.
<sometag>
<something>
.....
</something>
<item>item1
</item>
....
</sometag>
The following java code is not working:
String cleanxml = rawxml.replace("^[\\s\\S]+<item>", "");
What is the correct way to do this? And how do I address the non-greedy issue? Sorry I’m a C# programmer.
Well, if you want to use regex, then you can use
replaceAll. This solution uses a reluctant quantifier and a backreference:Alternately you can use
replaceFirst. This solution uses a positive lookahead.It makes more sense to just use
indexOfandsubstring, though.The reason why
replacedoesn’t work is that neithercharnorCharSequenceoverloads is regex-based. It’s simple character (sequence) replacement.Also, as others are warning you, unless you’re doing processing of simple XMLs, you shouldn’t use regex. You should use an actual XML parser instead.