-1
import re
file1 = open('/Users/sevi/Desktop/change.txt')
a = file1.readlines()
s = str(a)
s = re.sub(' so ', '. So, ', s)
print(s)

This code reads an all lowercase .txt file and changes every ' so ' with ' . So, '

However, I want this based on the nature of the word that comes next. In other words, I want this re.sub function not be applied when the ' so ' comes before a list of adjectives I have as a .rtf file.

For example, "so I changed my mind" will be "So, I changed my mind" But, "it was so fun" will be left untouched because fun is included in the list of words.

How can I make this possible?

Thanks!

1 Answers1

0
import re
s = (r'is a test so I changed my mind; but it was so fun').lower()
s = re.sub(' so (?!(nice|cold|hot|fun))', '. So,', s)
print(s)

?! is negative lookahead. The '|' is an alternative.

mcgrow
  • 1
  • 2