Compile replacement for Python's re.sub before the loop

I have trivial code to replace substrings using Python's re :

pattern = re.compile(r'(--type-header )([^ ]*)')
for x in somelist:
    filename = '...'  # here is a filename
    switches = x.replace('alice', 'bob')  # simple string
    switches = pattern.sub(
        r'\1' + f'{os.path.dirname(filename)}/' + r'\2',

The substring I'd like to replace:

--type-header cond_enum_04.h

Everything works like a charm on Linux/macOs. But on Windows I get:

re.error: bad escape \c at position 16

for about 250 iteration of the loop (249 iterations are successful). I suspect that this is a distinctive feature re in loops under the Windows. Is there some way to compile the replacement before entering the loop?