0

I am trying to replace / to null, like this

<h:graphicImage id=""> 

My code below.

<h:graphicImage id=""/>
<a>
<b>
<rich:componentControl target=""/>
<a>
<b>
</h:graphicImage>

The regex I used,

(<h:graphicImage[^>]*\s*)/>[\s\S]<rich:componentControl[^>]*)

It works if there no tags or content between the two tags but unfortunately in my code, there many tags between them. Any solution?

<h:graphicImage id=""/>
<rich:componentControl target=""/></h:graphicImage>

Edit

To be more clear about my question, I would like to replace

<h:graphicImage id=""/>  

to

<h:graphicImage id=""> 

when h:graphicImage tag has rich:componentControl this tag.

the code is like this below

<h:graphicImage id=""/>
<a>
<b>
<rich:componentControl target=""/>
<a>
<b>
</h:graphicImage>
  • 1
    What's your expected output? – Avinash Raj Mar 04 '15 at 08:22
  • 1
    You should use an XML parser (perhaps use XPath for match), trying to use regular expressions for XML is not a good idea. – greg-449 Mar 04 '15 at 08:29
  • 1
    Just a reminder: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 :-) – oopbase Mar 04 '15 at 08:43
  • Hello there, I clarified my question again. So the problem is I have to replace the symbol / when the h:graphicImage tag has rich:componentControl tag. The difficulty is the tags between them. I dont know how to ignore those tags – shu hung chou Mar 04 '15 at 08:43

1 Answers1

0

Don't use regex to parse XML or HTML That being said, this should work (with ungreedy/Lazy flag set to true):

(<h:graphicImage[^>.]*)\/>[.\S\s]*(<rich:componentControl[^>]*)

Check out: https://regex101.com/r/gQ2vP7/1

gijswijs
  • 1,958
  • 19
  • 24