简体   繁体   中英

How to replace words using regex with Xceed Docx

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);
 }

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

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM