Alex Rivera | Logout

Regex to match all HTML tags except <p> and </p>

Asked 2008-08-27T10:41:44.793
24

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

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

But this still matches with the closing </p> tag. Any hint on how to match with the closing tag as well?

Note, this is being performed on xhtml.

Edit
Report

1 Answer

0

Try this, it should work:

/<\/?([^p](\s.+?)?|..+?)>/

Explanation: it matches either a single letter except ā€œpā€, followed by an optional whitespace and more characters, or multiple letters (at least two).

/EDIT: I've added the ability to handle attributes in p tags.

answered 2008-08-27T10:47:17.697

Your Answer