0

i/p: <a><REF><a><REF><a></REF></REF><a>

Expected o/p: <b><REF><a><REF><a></REF></REF><b>

From the above string replace which is outside of ... tag with

Tried with below regex but its working only for one level but not for nested:

(<a>)(?!(.(?!<REF>))*?(<\/REF>))

Given regex is working for <a><REF><a><?REF><a> but its failing with <a><REF><a><REF><a></REF></REF><a>.

Actual String: <a><REF><a><REF><a><BR><REF><BR></REF><a></REF></REF><a>

Expected String: <b><REF><a><REF><a><BR><REF><BR></REF><a></REF></REF><b>

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Darshan
  • 35
  • 5
  • Note the `(?!(.(?!))*?` is a coorupt tempered greedy token. It must be written as `(?:(?!).)*?`, but certainly, it won't help with nested patterns (unless you remove them from the string, but you are not). – Wiktor Stribiżew Oct 17 '19 at 10:11
  • 3
    I remember seeing this exact question yesterday, [did you delete and re-post it](https://stackoverflow.com/questions/58411489/replace-a-text-or-tag-which-is-not-present-inside-specific-tagcould-be-nested)? – JonK Oct 17 '19 at 10:14
  • @JonK weird, it's been marked as dupe by Wiktor, deleted by OP and now reposted indeed. – Mena Oct 17 '19 at 10:19
  • @JonK Yes I deleted and re posted with the proper example. – Darshan Oct 17 '19 at 10:26
  • @curiosa that is not the answer for my question. – Darshan Oct 17 '19 at 10:28
  • Well, it should be. It is just not possible to achieve this with regex, even if you don't want to belive it. The link provides [another link](http://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la) to the detailed explanation why. – Curiosa Globunznik Oct 17 '19 at 10:34
  • Well, still its probably not exactly helpful. I found [this post](https://stackoverflow.com/questions/52260440/renaming-all-xml-tag-names-in-java/52261115) that does recursive search/replace in xml, [that one](https://stackoverflow.com/questions/6382111/updating-a-property-of-a-xml-tag) too, you should be able to adapt it to your issue. – Curiosa Globunznik Oct 17 '19 at 10:41
  • @Darshan definitely agree on the sentiment here that regex is not the right tool for what you're trying to do. Whether or not your specific requirement is actually achieveable with regex is beyond my willingness to investigate, but the solution will likely be unpalatable. You probably want a specific parser for that - there are many frameworks you can use for markup. – Mena Oct 17 '19 at 10:54
  • @curiosa [link](http://stackoverflow.com/questions/6751105/why-its-not-possible-to-use-regex-to-parse-html-xml-a-formal-explanation-in-la) is useful. And my XML is in string format, if it is not at all possible then will go with alternate solutions. – Darshan Oct 17 '19 at 10:54
  • @Mena yes i am thing out for other way solution, please let me know the frame works which I can use to this problem. Thanks! – Darshan Oct 17 '19 at 10:57
  • What about the java [dom parser](https://howtodoinjava.com/xml/read-xml-dom-parser-example/)? Or [Antlr](https://www.antlr.org/), its fun? – Curiosa Globunznik Oct 17 '19 at 11:00
  • @Darshan SAX, StAX and DOM come to mind. You could also use Jackson XML at higher level but that's probably too much overhead. Or you could build your own parser if this is only a tiny, specific case. Happy hunting. – Mena Oct 17 '19 at 11:00
  • Possible duplicate of [Remove XML Tag and Content in XML String using Java Regex](https://stackoverflow.com/questions/42894585/remove-xml-tag-and-content-in-xml-string-using-java-regex) – sayalok Oct 17 '19 at 11:15
  • Thanks for the suggestions curiosa and Mena, will try it out. – Darshan Oct 17 '19 at 11:18
  • @sayalok not the one I am looking for. – Darshan Oct 17 '19 at 11:19
  • I have a great HTML parse package. It's posted in the answer below - and performs the substitution just fine. –  Oct 17 '19 at 12:01
  • Output: `

    ` and also: `Equals Desired Result? true`
    –  Oct 17 '19 at 12:03
  • For future reference, deleting and reposting questions can land you in trouble - if you've missed something from a question then your best bet is to use the edit function to update your original question with the missing info. – JonK Oct 17 '19 at 12:33
  • Question was not deleted by the poster of the question. –  Oct 17 '19 at 12:45

0 Answers0