python regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    python regular expression

    I am trying to edit a bunch of files that are similar. I want to remove all
    the ASP code that appears before the <HTML> tag. Can some one help me with a
    regex that can replace everything before the <HTML> tag with nothing?




  • Andrew Dalke

    #2
    Re: python regular expression

    eddie:[color=blue]
    > Can some one help me with a
    > regex that can replace everything before the <HTML> tag with nothing?[/color]
    [color=blue][color=green][color=darkred]
    >>> pat = re.compile(r"^( (?!<HTML).)*", re.IGNORECASE | re.DOTALL)
    >>> pat.sub("", "junk\n<HTML>st uff")[/color][/color][/color]
    '<HTML>stuff'[color=blue][color=green][color=darkred]
    >>> pat.sub("", " <html>stuff")[/color][/color][/color]
    '<html>stuff'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]


    Andrew
    dalke@dalkescie ntific.com


    Comment

    • Peter Hansen

      #3
      Re: python regular expression

      eddieNOSPAM@edd iecentral.net wrote:[color=blue]
      >
      > I am trying to edit a bunch of files that are similar. I want to remove all
      > the ASP code that appears before the <HTML> tag. Can some one help me with a
      > regex that can replace everything before the <HTML> tag with nothing?[/color]

      stuff = 'whatever ASP blah\nblah <HTML>more blah blah</HTML>maybe even more'
      try:
      shortStuff = stuff[stuff.index('<H TML>'):]
      except:
      shortStuff = stuff

      No regex required...

      Comment

      • Gary Herron

        #4
        Re: python regular expression

        On Friday 21 November 2003 03:03 pm, eddieNOSPAM@edd iecentral.net wrote:[color=blue]
        > I am trying to edit a bunch of files that are similar. I want to remove all
        > the ASP code that appears before the <HTML> tag. Can some one help me with
        > a regex that can replace everything before the <HTML> tag with nothing?[/color]

        You don't need a regular expression for that. Just find the index of
        the first occurrence of <HTML> and slice away.

        i = data.find('<HTM L>') # i=-1 means not found
        if (i != -1)
        data = data[i:]

        Gary Herron



        Comment

        Working...