How to sub with matched groups and variables in Python
Posted
by
Syed
on Stack Overflow
See other posts from Stack Overflow
or by Syed
Published on 2011-03-16T21:26:00Z
Indexed on
2011/03/17
0:10 UTC
Read the original article
Hit count: 183
Hi, new to python. This is probably simple but I haven't found an answer.
rndStr = "20101215"
rndStr2 = "20101216"
str = "Looking at dates between 20110316 and 20110317"
outstr = re.sub("(.+)([0-9]{8})(.+)([0-9]{8})",r'\1'+rndStr+r'\2'+rndStr2,str)
The output I'm looking for is:
Looking at dates between 20101215 and 20101216
But instead I get:
P101215101216
The values of the two rndStr's doesn't really matter. Assume its random or taken from user input (I put static vals here to keep it simple). Thanks for any help.
© Stack Overflow or respective owner