Home > Software design >  Regex to match a string if not followed by another string
Regex to match a string if not followed by another string

Time:08-24

In Mediawiki via Replace extension (MariaDB 10.6) I want to match the string <span ><span >A</span> and delete it, as long as there is no <span > further down that line. Here is an example of text where it should not be matched:

<span ><span >A</span> [[lay bare at the side]], [[expose]], τι τῆς πλευρᾶς <span >Arr. <span >Tact.</span>40.5</span>, cf. <span >D.C.49.6</span> (Pass.). </span><span ><span >2</span> metaph., [[lay bare]], [[disclose]], τὸν πάντα λόγον <span >Hdt.1.126</span>, cf. <span >8.19</span>, <span >9.44</span>; τὸ βούλευμα <span >Conon 50</span>:—Pass., <b >παρεγυμνώθη διότι</b>… <span >Plb.1.80.9</span>.</span>

So far I tried (<span ><span >A<\/span>) ((?!<span >).*) (and replacing with nothing) but it matches instances that do contain the unwanted string.

CodePudding user response:

You can use

<span ><span >A<\/span>(?s)(?!.*<span >)

See the regex demo. Details:

  • <span ><span >A<\/span> - a literal <span ><span >A</span> string
  • (?s) - s flag that makes . match across lines
  • (?!.*<span >) - a negative lookahead that fails the match if, immediately to the right of the current location, there are
    • .* - any zero or more chars as many as possible
    • <span > - a literal string.
  • Related