Su4p
Su4p

Reputation: 865

regex PHP find string and delete parent

in a very big string I have to delete the [w:r][/w:r] where the substring "delete" exist. Example -of substring I want to delete - :

[w:r w:rsidR="00A37EED" w:rsidRPr="00FE1BE1"][w:rPr][w:b][/w:rPr][w:t]delete[/w:t][/w:r]

This one is my best guess \[w:r.*delete.*\[\/w:r\] I tried multiple regex expression but it's not my strong suit.

I copy-pasted the string on regex101 here's the link https://regex101.com/r/wS4bL2/1

I succeeded at finding the required pattern but I can't make it stop at the first occurence of [/w:r].

PHP code -in case you are wondering- :

$this->tempDocumentMainPart = preg_replace('/\[w:r.*delete.*\[\/w:r\]/','',$this->tempDocumentMainPart);

Upvotes: 1

Views: 60

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626709

The .* will overflow across the [....]s. One way is to use a tempered greedy token:

\[w:r\b(?:(?!\[w:r\b).)*?delete(?:(?!\[w:r\b).)*?\[\/w:r]
        ^^^^^^^^^^^^^^^^^       ^^^^^^^^^^^^^^^^^

See the regex demo

The (?:(?!\[w:r\b).)*? tempered greedy token will limit matching inside one [w:r (that has a word boundary on the right).

Add a DOTALL modifier /s ('/PATTERN/s') so as to match across newlines.

Upvotes: 2

Related Questions