tags:

views:

82

answers:

2

I want to use this regular expression in Python:

 <(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>

(from http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/)

def removeHtmlTags(page):
    p = re.compile(r'XXXX')
    return p.sub('', page)

It seems that I cannot directly substitute the complex regular expression into the above function.

+2  A: 

Works fine here. You're probably having trouble because of the quotes. Just triple-quote it:

def removeHtmlTags(page):
    p = re.compile(r'''<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>''')
    return p.sub('', page)
Ignacio Vazquez-Abrams
A: 

If you need to remove HTML tags, this should do it:

import re

def removeHtmlTags(page):
    pattern = re.compile(r'\<[^>]+\>', re.I)
    return pattern.sub('', page)
Jaú
That wasn't the question, but the point of the original regex is to allow for angle brackets within attribute values.
Alan Moore