regexp search for variable and string

John Machin sjmachin at lexicon.net
Sun May 19 08:08:46 EDT 2002


lazerpub at yahoo.com (Joshua Newman) wrote in message news:<a6fc297e.0205181944.12e25c68 at posting.google.com>...
> Sorry if this has been asked and answered innumerable times.  I
> couldn't find the answer anywhere.
> 
> I'm trying to grab some news with wget and python.
> I can't regexp for a variable (date) and specific words in the same
> re.compile expression.  Any hints?

YES. See below.

> 
> #get today's date
> date= time.strftime("%Y/%m/%d", time.localtime(time.time()))
> 
> #construct a string to search for today's date and wire services but no
> #sports or index pages
> restring="""r'("""+date+"""|aponline|reuters)(?!/sports|.*index.html)'"""

Hint: insert a print statement here to show exactly what restring is bound to.

> today=re.compile(restring)
> 
> #above doesn't work, but:

Not surprising; it's reutered.

> #today=re.compile(r'(2002/05/19|aponline|reuters)(?!/sports|.*index.html)'
> #works fine with:
> todaysurl=re.findall(today,onlinenewspaperURLs)
> 
> Can I do this in one regexp?

Of course you can. You just have to build the regexp correctly.



More information about the Python-list mailing list