0

I have tried to get this working for most of the day. At this point I can't even remember what I have tried and what I haven't tried, so now I'm asking for help!

I'm going to be using php's preg_replace() to delete stanzas out of svg code.

But the problem I am facing can be shown with this much simpler example:

<div>
TRUE
</div>

<div>
FALSE
</div>

<div>
MAYBE
</div>

How do I match the middle div? A simple version of my regular expression is

(?s)<div.*?FALSE.*?<\/div>

That doesn't work, as it will match the first 2 divs, instead of only the middle one.

(is it still called greedy when it expands the match to the left?)

All the variations I tried got me nowhere. I'm sure there is a simple answer, I just couldn't find it on my own.

benvc
  • 14,448
  • 4
  • 33
  • 54
Norsak
  • 9
  • 1
  • 4
  • 1
    And now, [it works](https://regex101.com/r/INCWGr/2). – Wiktor Stribiżew Oct 30 '18 at 14:10
  • 1
    Sorry, didn't even see your answer. Yes that is spot on. I don't post questions often, 3min from Q to A is better than I could have imagined. – Norsak Oct 30 '18 at 15:20
  • Please remove this duplicate post. There are a lot of such questions. [Here](https://stackoverflow.com/questions/27385942/why-is-this-simple-non-greedy-regex-being-greedy) is one although [mine](https://stackoverflow.com/questions/37240408/regular-expressions-ensuring-b-doesnt-come-between-a-and-c/37240700#37240700) is quite generic. – Wiktor Stribiżew Oct 31 '18 at 11:36

1 Answers1

-1

Ok, of course I found a solution shortly after I posted the question.

This one works

Basically the original regex started with

<div.*?

And the correct answer was to replace that . with

(?:.(?!<div))

Which is a non-capture group, using . with a negative look-ahead for <div Since neither a look-ahead nor a look-behind can be part of a match .... This amounts to: match "Anything" except <div

Norsak
  • 9
  • 1
  • 4