Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python regular expression

Reply
Thread Tools

python regular expression

 
 
Guest
Posts: n/a
 
      11-21-2003
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?




 
Reply With Quote
 
 
 
 
Andrew Dalke
Guest
Posts: n/a
 
      11-21-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      11-21-2003
On Friday 21 November 2003 03:03 pm, (E-Mail Removed) 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



 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      11-21-2003
(E-Mail Removed) 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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments