I want to make a Python script that creates footnotes. The idea is to find all strings of the sort "Some body text.{^}{Some footnote text.}" and replace them with "Some body text.^#", where "^#" is the proper footnote number. (A different part of my script deals with actually printing out the footnotes at the bottom of the file.) The current code I'm using for this is:

pattern = r"\{\^\}\{(.*?)\}"
i = 0
def create_footnote_numbers(match):
   global i
   i += 1
   return "<sup>"+str(i)+"</sup>"

new_body_text = re.sub(pattern, create_footnote_numbers, text)

This works fine, but it seems weird to have to declare a variable (i) outside the create_footnote_numbers function and then have to call it inside that function. I would have thought there'd be something inside re that would return the number of the match.

Edit
Report