I will assume that you want to be doing this matching on a line by line basis. The best way to describe how you might go about how to do this is with an example. Let's say I have the following file, test.txt
:
{'name': 'Bryan', 'age': 34, 'male': True, 'hometown': 'Boston'}
{'name': 'Anna', 'age': 25, 'male': False, 'hometown': 'Chicago'}
{'name': 'Jeff', 'age': 47, 'male': True, 'hometown': 'Vancouver'}
{'name': 'Maria', 'age': 58, 'male': False, 'hometown': 'Madrid'}
For each line I want to match whatever does not match the regular expression:
r" 'age': \d+,"
So for the first line, that would be:
{'name': 'Bryan', 'male': True, 'hometown': 'Boston'}
In essence we are just replacing the regular expression r" 'age': \d+,"
with an empty string, so:
import re
pattern = re.compile(r" 'age': \d+,")
with open('test.txt') as f:
for line in f:
line = pattern.sub(r'', line)
print(line, end='')
Prints:
{'name': 'Bryan', 'male': True, 'hometown': 'Boston'}
{'name': 'Anna', 'male': False, 'hometown': 'Chicago'}
{'name': 'Jeff', 'male': True, 'hometown': 'Vancouver'}
{'name': 'Maria', 'male': False, 'hometown': 'Madrid'}
Summary
Search for your regex and replace it by an empty string. What's left is equivalent to having matched everything that was the complement of the regex.