regex - How to sub with matched groups and variables in Python -


New in Python This is probably easy, but I could not find any answer. Viewing dates between 20110316 and 20110317 "outstr = re.sub (" (. +) ([0- 9]

  rndStr = "20101215" rndStr2 = "20101216" str = {8}) (. +) ([0- 9] {8}) ", r '\ 1' + rndStr + r '\ 2' + rndStr2, str) < 
  20101215 And see the date between 20101216   

but instead I get:

  P101215101216   

two rndStr The value of does not really matter. Take your random value or taken from user input (I put static vals here to keep it steady). Thanks for any help.

Your backtraces are unclear your replacement string

  \ 120101215 \ 220101216   

There are two large numbers for backtracking.)

To resolve this, use this syntax:

  r '\ G  1 & gt;' + RndStr + r '\ g  + RndStr2 +   

If you have too many sets (or "brackets" if you speak British English like mine :) - you [0- 9] {8} Brackets around parts are not required, which you are not referring again:

  re.sub ("(. +) [0- 9] {8} ( . +) [0- 9] {8} ", ...   

should be sufficient.

(And, as mentioned elsewhere, Do not use str as a variable name. Why do not you debug age? Wish, code> str.replace () does not work anymore. It is not that I used to do this anyway ... noooo.):

Then the whole thing goes: import re rndStr = "20101215" rndStr2 = "20101216" s = "Looking at dates between 20110316 and 20110317" outstr = re.sub ("(. +) [0- 9] {8} (. +) [0- 9] {8} ", R & G; 1> '+ RDSR + R' \ g <2> gt; + +

Build:

  View date between 20101215 and 20101216    

Comments