0
votes

I have multiple words in a word document and they all start and end with "$", for example $name$. I'm trying to replace all such occurrences with an empty string. How can I go about this? Snippet of what I tried below and nothing happened. I'm using the free version.

using (DocX document = DocX.Load("Example.docx"))
 {
    String pattern = Regex.Escape("$") + ".+" + Regex.Escape("$");
    document.ReplaceText(pattern, "",false, RegexOptions.IgnoreCase);
 }
2
@PrasadTelkikar I just tried and didnt work.West

2 Answers

0
votes

Try this pattern: \$.+?\$ In your code it can looks like:

String pattern = Regex.Escape("$") + ".+?" + Regex.Escape("$");
0
votes

Okay looks like ReplaceText expects a function when using regex. I've got it working

string WordCheck(string find)
{
 return "";
}

String pattern = Regex.Escape("$") + ".+?" + Regex.Escape("$");
document.ReplaceText(@"\$.+?\$",WordCheck,false, RegexOptions.IgnoreCase);