Added word boundaries

This commit is contained in:
Tiago Ribeiro
2024-02-05 22:48:59 +00:00
parent 3a1dc33e1b
commit 9149e4b197

View File

@@ -49,7 +49,7 @@ def fix_exercise_ids(exercise, start_id):
def replace_first_occurrences_with_placeholders(text: str, words_to_replace: list, start_id):
for i, word in enumerate(words_to_replace, start=start_id):
# Create a case-insensitive regular expression pattern
pattern = re.compile(re.escape(word), re.IGNORECASE)
pattern = re.compile(r'\b' + re.escape(word) + r'\b', re.IGNORECASE)
placeholder = '{{' + str(i) + '}}'
text = pattern.sub(placeholder, text, 1)
return text