Stripping HTML with RE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steveo

    #1

    Stripping HTML with RE

    I am currently stripping HTML from a string with the following code.
    (I know it's not the best way to strip HTML but bear with me)

    re.compile("<.* ?>")

    I wanted to allow all H1 and H2 tags so i changed it to:

    re.compile("<[^H1|^H2]*?>")

    This seemed to work but it also allowed the HTML tag(basically anythin
    with an H or a 1 or a 2) How can I get this to strip all tags except
    H1 and H2. Any Help you could give would be great.

    Steve
  • Steven Bethard

    #2
    Re: Stripping HTML with RE

    Steveo <stephen_p_barr ett <at> hotmail.com> writes:[color=blue]
    >
    > I wanted to allow all H1 and H2 tags so i changed it to:
    >
    > re.compile("<[^H1|^H2]*?>")
    >
    > This seemed to work but it also allowed the HTML tag(basically anythin
    > with an H or a 1 or a 2) How can I get this to strip all tags except
    > H1 and H2. Any Help you could give would be great.[/color]

    You probably want a lookahead assertion. From the docs at


    (?!...)
    Matches if ... doesn't match next. This is a negative lookahead assertion.
    For example, Isaac (?!Asimov) will match 'Isaac ' only if it's not followed by
    'Asimov'.

    So I would write your example something like:
    [color=blue][color=green][color=darkred]
    >>> re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<a>sdfsa</a>')[/color][/color][/color]
    'sdfsa'[color=blue][color=green][color=darkred]
    >>> re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<H1>sdfsa</a>')[/color][/color][/color]
    '<H1>sdfsa'[color=blue][color=green][color=darkred]
    >>> re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<H1>sdfsa</H2>')[/color][/color][/color]
    '<H1>sdfsa</H2>'

    (I was too lazy to compile the re, but of course that's what you'd normally want
    to do.)

    Steve

    Comment

    • Miles Fender

      #3
      Re: Stripping HTML with RE

      Steveo wrote:[color=blue]
      > I am currently stripping HTML from a string with the following code.
      > (I know it's not the best way to strip HTML but bear with me)
      > [...][/color]

      Instead of using REs, you might consider the StrippingParser
      from the Python Cookbook:



      It allows you to specify explicitly which tags you want to leave
      intact, so you'll be able to change your mind later without futzing
      about with a complex RE...


      Miles

      Comment

      • Steven Bethard

        #4
        Re: Stripping HTML with RE

        I wrote:[color=blue][color=green][color=darkred]
        > >>> re.sub(r'</?(?!H1|H2|/H1|/H2)[^>]*>', r'', '<a>sdfsa</a>')[/color][/color]
        > 'sdfsa'[/color]

        Maybe slightly better:
        [color=blue][color=green][color=darkred]
        >>> re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<a>sdfsa</a>')[/color][/color][/color]
        'sdfsa'[color=blue][color=green][color=darkred]
        >>> re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<H1>sdfsa</a>')[/color][/color][/color]
        '<H1>sdfsa'[color=blue][color=green][color=darkred]
        >>> re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<H1>sdfsa</H2>')[/color][/color][/color]
        '<H1>sdfsa</H2>'[color=blue][color=green][color=darkred]
        >>> re.sub(r'<(?!/?(?:H1|H2))[^>]*>', r'', '<H2>sdfsa</H2>')[/color][/color][/color]
        '<H2>sdfsa</H2>'

        I've just grouped things a bit differently so that I only have to write H1 and
        H2 once.

        Steve

        Comment

        Working...