0

I'm struggling with Regex and it's not going well.

Let's say I have this:

<div class="abc">
    <div class="def">
        <div class="ghi">
            <div class="jkl">
                ...
            </div>
        </div>
    </div>
</div>

I want to match <div class="def"> and that </div> before the last one. How can I do that with regex? I use this site for help: https://regex101.com/

that's what I have now

This is my regex code: /<div class="def">(.*)<\/div>/isU

But it will not match the </div> before the last one.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
wwwampy
  • 1
  • 3
  • 1
    You can’t do this sort of indeterminate-depth nested match with regex. Take a look at https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1758162#1758162. What language are you using? there are mechanisms. – John Williams Mar 04 '23 at 15:48
  • Hmm, ok, thank you. I'm just trying to create a template modification with regex in XenForo software. I thought this is possible so I don't have to find/replace a bunch of code (not convenient). – wwwampy Mar 04 '23 at 16:25
  • I'm just adding my "don't do this". May be better off using something like BeautifulSoup from Python's bs4 package. – SSlinky Mar 04 '23 at 16:28
  • Just to add... I am trying to edit a simple HTML template with a regex HTML template modification. But from the comments, I can see I rather not do it or it's not even possible. – wwwampy Mar 04 '23 at 16:31
  • HTML is (mostly) xml. I suspect you can manipulate it like this - https://blog.teamtreehouse.com/how-to-parse-xml-with-php5?amp=1 – John Williams Mar 04 '23 at 19:14
  • If you included a programming language, it would be possible to do this with regex. – RifloSnake Mar 04 '23 at 23:06

0 Answers0