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: A good regex tester:

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)


# 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#:

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

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