user3325376 user3325376 - 1 year ago 39
PHP Question

regex not stop when using dot matches

I want to remove some scripts from pages that contains "site" word

bla bla bla
</script><p>this is line></p>

<script> !

bla bla bla


aaa bla bla bla

i used this regex


but unfortunately it contains lines do not related to the matches i want

debug link :

how can i stop regex when it's find

p.s : i want to match all
at once

Answer Source

Confusing looks, that you have some scritp and some script in your sample & demo. Is this meant? However you can use a negative lookahead if this would be convenient for your input:

  • ((?!</script).)*? matches lazily any amount of characters not followed by </script
  • until site and (?1) reuses the pattern in first group until </script> greedily.

More explanation and demo at regex101

For this kind of problems usually a parser solution is be to be preferred. Depends on input.