Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace text using regular expressions in MS Word - C#

Tags:

c#

regex

ms-word

I have a Word document that I want to open and replace all instances of a social security number with the word, "test".

I already have the code to open the document. Here's the code that would do the replacement. However, I'm having trouble with using regular expressions at this point: _wordApp.Selection.Find.Text = ; within my code. Is using regular expressions a good approach or is there a better approach? Bear in mind, I have to match any social security number... hence: \b[0-9]{3}-[0-9]{2}-[0-9]{4}\b OR \b[0-9]{3}[0-9]{2}[0-9]{4}\b

Thanks in advance...

object replaceAll = Werd.WdReplace.wdReplaceAll;

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.Text = ;

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";

_wordApp.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                ref replaceAll, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
like image 491
Woody Avatar asked Sep 30 '09 19:09

Woody


People also ask

How do I use find and Replace in a regular expression in word?

Word supports find/replace with it own variation of regular expressions (regex), which is called wildcards. To use regex: Ctrl-H (Find/Replace) ⇒ Check "Use wildcards" option under "More". Read "Regular Expression (Regex)" for the syntax of Regex.

How does regex replace work C#?

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string. In a specified input substring, replaces a specified maximum number of strings that match a regular expression pattern with a specified replacement string.

Can you replace text in word?

Note: In Word for the web, select Next result to find where your text appears in a file. In the Replace with box, type the text you want. Select Replace to change the text or select Replace All to change all instances of this text in your file.


1 Answers

MS Word has a wildcard matching capability built in. It's not as powerful as regular expressions, but it looks like it's able to match simple patterns like social security numbers.

(The < and > wildcards match start and end word boundaries.)

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.MatchWildcards = true;
_wordApp.Selection.Find.Text = "<[0-9]{3}-[0-9]{2}-[0-9]{4}>"; // SSN with dashes.

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.MatchWildcards = true;
_wordApp.Selection.Find.Text = "<[0-9]{9}>"; // SSN without dashes.

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";
like image 137
Chris R. Timmons Avatar answered Sep 30 '22 15:09

Chris R. Timmons