python regular expression

Discussion in 'Python' started by Guest, Nov 21, 2003.

  1. Guest

    Guest Guest

    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?
     
    Guest, Nov 21, 2003
    #1
    1. Advertising

  2. Guest

    Andrew Dalke Guest

    eddie:
    > Can some one help me with a
    > regex that can replace everything before the <HTML> tag with nothing?


    >>> pat = re.compile(r"^((?!<HTML).)*", re.IGNORECASE | re.DOTALL)
    >>> pat.sub("", "junk\n<HTML>stuff")

    '<HTML>stuff'
    >>> pat.sub("", " <html>stuff")

    '<html>stuff'
    >>>



    Andrew
     
    Andrew Dalke, Nov 21, 2003
    #2
    1. Advertising

  3. Guest

    Gary Herron Guest

    On Friday 21 November 2003 03:03 pm, wrote:
    > 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?


    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('<HTML>') # i=-1 means not found
    if (i != -1)
    data = data[i:]

    Gary Herron
     
    Gary Herron, Nov 21, 2003
    #3
  4. Guest

    Peter Hansen Guest

    wrote:
    >
    > 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?


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

    No regex required...
     
    Peter Hansen, Nov 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,379
  2. newPeter

    Regular Expression in Python

    newPeter, Nov 10, 2004, in forum: Java
    Replies:
    2
    Views:
    360
    Chris Smith
    Nov 10, 2004
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    881
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    12,046
    vforvikash
    Dec 29, 2008
  5. K_Lee
    Replies:
    18
    Views:
    619
    Alex Martelli
    Nov 13, 2003
Loading...

Share This Page