开发者

Regular expression to match closing HTML tags

I'm working on a small Python script to clean up HTML documents. It works by accepting a list of tags to KEEP and then parsing through the HTML code trashing tags that are not in the list I've been using regular expressions to do it and I've been able to match opening tags and self-closing tags but not closing tags.

The pattern I've been experimenting with to match closing tags is </(?!a)>. This seems logical to me so why is not working? The (?!a) should match on anything that is NOT an anchor tag (not that 开发者_运维知识库the "a" is can be anything-- it's just an example).

Edit: AGG! I guess the regex didn't show!


  1. Read:

    • RegEx match open tags except XHTML self-contained tags
    • Can you provide some examples of why it is hard to parse XML and HTML with a regex?
  2. Repent.

  3. Use a real HTML parser, like BeautifulSoup.


<TAG\b[^>]*>(.*?)</TAG> 

Matches the opening and closing pair of a specific HTML tag.

<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>

Will match the opening and closing pair of any HTML tag.

See here.


Don't use regex to parse HTML. It will only give you headaches.

Use an XML parser instead. Try BeautifulSoup or lxml.


You may also consider using the html parser that is built into python (Documentation for Python 2 and Python 3)

This will help you home in on the specific area of the HTML Document you would like to work on - and use regular expressions on it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜