Search code examples
pythonstringstrip

python re prevent stripping whitespaces


:) I'm not sure why the following python code removes whitespaces too, but it does. Could someone please explain how I could pull this off without it doing so? Thank you ! :)

text = html
rules = [
    { r'>\s+' : u'>'},
    { r'\s+' : u' '},
    { r'\s*<br\s*/?>\s*' : u'\n'},
    { r'</(div)\s*>\s*' : u'\n'},
    { r'</(p|h\d)\s*>\s*' : u'\n\n'},
    { r'<head>.*<\s*(/head|body)[^>]*>' : u'' },
    { r'<a\s+href="([^"]+)"[^>]*>.*</a>' : r'\1' },
    { r'[ \t]*<[^<]*?/?>' : u'' },
    { r'^\s+' : u'' }
]
for rule in rules:
    for (k,v) in rule.items():
        regex = re.compile (k)
        text  = regex.sub (v, text)
print text

Solution

  • As you can read in the docs: http://docs.python.org/library/re.html

    The \s sequence matches all whitespace. So the bottom rule will remove all whitespace.