pattern match

Adam Hupp hupp at cs.wisc.edu
Tue Apr 22 21:31:53 EDT 2003


On Wed, Apr 23, 2003 at 12:52:38AM +0000, Mitch MF Cummstain wrote:
>                I just started using python and I need help with a
> pattern match.
>       
>    m = re.match("(.{30})(word)(.{30})",paragraph);
> 
> I need to match 30 characters on each side of word.  The problem word
> is getting treated as a string not a variable like I need it to.  How
> can I make it treat word as a variable ?

word = "foo"

pat = "(.{30})(%s)(.{30})" % word

m = re.match(pat, paragraph)


The '%' operator on a string acts like sprintf, it inserts it's
right hand side into the string at the point '%s' (s for string, d for
integer, etc).  

It's not necessary to put a semicolon at the end of a statement, BTW.

-Adam





More information about the Python-list mailing list