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.
Your backreferences are ambiguous. Your replacement string becomes
which is two rather large numbers to be backreferencing 🙂
To solve it, use this syntax:
You also have too many sets of parentheses (or “brackets” if you speak British English like me:) – you don’t need parentheses around the
[0-9]{8}parts which you’re not backreferencing:should be sufficient.
(And, as noted elsewhere, don’t use
stras a variable name. Unless you want to spend ages debugging whystr.replace()doesn’t work anymore. Not that I ever did that once… noooo. 🙂so the whole thing becomes:
Producing: