A simple question in regex:
I want to replace page numbers in a string with pagenumber + some number (e.g. 10). I decided that I could capture the matching page number using a backlink, perform an operation on it, and use it as a replacement argument in re.sub .
This works (just passing the value):
def add_pages(x): return x re.sub("(?<=Page )(\d{2})",add_pages(r"\1") ,'here is Page 11 and here is Page 78\nthen there is Page 65',re.MULTILINE)
Harvest, of course, 'here is Page 11 and here is Page 78\nthen there is Page 65'
Now, if I change the add_pages function to change the passed backlink, I get an error.
def add_pages(x): return int(x)+10 re.sub("(?<=Page )(\d{2})",add_pages(r"\1") ,'here is Page 11 and here is Page 78\nthen there is Page 65',re.MULTILINE) ValueError: invalid literal for int() with base 10: '\\1'
because what is passed to the add_pages function seems to be a literal backlink, not what it refers to.
There is no retrieval of all the associated numbers in the list, and then processing and adding back, how would I do this?
source share