regexp search for variable and string

John La Rooy larooy at xtar.co.nz
Sun May 19 03:40:04 EDT 2002


On 18 May 2002 20:44:32 -0700
lazerpub at yahoo.com (Joshua Newman) wrote:

> 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?
> 
> #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)'"""
> today=re.compile(restring)
> 
> #above doesn't work, but:
> #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?
> Thanks for the help
> 
> Joshua

You're using too many quotes. Why not just

today = re.compile(r'(%s|aponline|reuters)(?!/sports|.*index.html)'%date)
or
today = re.compile(r'('+date+r'|aponline|reuters)(?!/sports|.*index.html)')

there's also no need to use raw strings here.

John



More information about the Python-list mailing list