Regex to match all HTML tags except

and

后端 未结 13 751
抹茶落季
抹茶落季 2020-11-30 06:31

I need to match and remove all tags using a regular expression in Perl. I have the following:

<\\\\??(?!p).+?>

But this still matche

13条回答
  •  旧巷少年郎
    2020-11-30 07:09

    The original regex can be made to work with very little effort:

     <(?>/?)(?!p).+?>
    

    The problem was that the /? (or \?) gave up what it matched when the assertion after it failed. Using a non-backtracking group (?>...) around it takes care that it never releases the matched slash, so the (?!p) assertion is always anchored to the start of the tag text.

    (That said I agree that generally parsing HTML with regexes is not the way to go).

提交回复
热议问题