Ooops sorry, for the regex-function, you don't want to increment the existing chapter number, but to create a number from 1 to x, so the code would be (saying the form is "<h1>Chapter 1</h1>")
Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
data.setdefault('chapnum', 0)
if match.group(2):
data['chapnum'] += 1
return match.group(1) + str(data['chapnum']) + match.group(3)
return match.group(0)