replacing words in a word document causes multiple replacements with c characters

If you already have some script for this, feel free to post it and I’ll try and help more.

I’m not sure what functionality you’re using to find the text instance, but I would suggest looking into regex, and using something like (LeadSoft IT(?! Limited)).

Regex: https://regexr.com/ A good regex tester: https://www.regextester.com/109925

Edit: I made a Python script that uses regex to replace the instances:

import re

word_doc = "We like working " \
           "here at Leadsoft IT.\n" \
           "We are not limited here at " \
           "Leadsoft It Limited."

replace_str = "Leadsoft IT Limited"

reg_str = '(Leadsoft IT(?!.?Limited))'

fixed_str = re.sub(reg_str, replace_str, word_doc, flags=re.IGNORECASE)

print(fixed_str)

# Prints:
# We like working here at Leadsoft IT Limited.
# We are not limited here at Leadsoft It Limited.

Edit 2: Code re-created in C#: https://gist.github.com/Zylvian/47ecd6d1953b8d8c3900dc30645efe98

The regex checks the entire string for instances where Leadsoft IT is NOT followed by Limited, and for all those instances, replaces Leadsoft IT with Leadsoft IT Limited.

The regex uses what’s called a “negative lookahead (?!)” which makes sure that the string to the left is not followed by the string to the right. Feel free to edit the regex how you see fit, but be aware that the matching is very strong.

If you want to understand the regex string better, feel free to copy it into https://www.regextester.com/.

Let me know if that helps!

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top