Replace all tags except one with regexp in Java
•
Java
I have the following questions I want to delete all substrings starting with < And ends with > except substring < back >
Example: < apps > < Max > < down > < capital >... Should be deleted, but not < back >
I'm sure this applies to regexp and string Replace (), but I don't know how
At present, I have thought of this:
line = line.replaceAll("<[^<]*>","");
The problem is that this will also delete < back > - substring!
I hope one of you knows the solution
Thanks for your help!
Solution
You can use (?! < back >) < [^ <] * >, line = line replaceAll(“(?!< back>)< [^<] *>”,“”);
(?! < back >) (negative forward looking) does not match the label < back >
RegEx Demo
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码