While trying to set up a verbose regex:
# set up variables
ankerwords = ['beerdigt','bestattet','begraben','beigesetzt']
# combine the words, five words before/after
rx = re.compile(r'''
(?:\b\w+\W+){5} # five words before
(?:{})
(?:\W+\w+\b){5} # five words thereafter
'''.format("|".join(ankerwords)), re.X)
This throws an error IndexError: tuple index out of range
.
I know it's because of the
{5}
in the expression but how to get around it without splitting the string in several parts, i.e.
'''(?:\b\w+\W+){5}''' + '(?:{})'.format(...)
It's more a question of style, really.