Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Apr 26, 2016 3:12 pm

Hi,

I have several "placeholders" in my Word documents. In fact these placeholders are just simple words in the text that all look like this: <blablabla>.

Examples:
    <Name>
    <Street>
    <Zip_City>

My goal is to replace allof such placeholders with an empty string. Here is my current code:

Code: Select all
var searchString = @"\<(.*?)\>";
var replaceString = string.Empty;
document.Replace(matchString: searchString, newValue: replaceString, caseSensitive: false, wholeWord: true);


Unfortunately, none of the placeholders gets replaced this way. Am I using a wrong regular expression? Or does Spire.Doc 5.6.75.4020 just not Support such scenarios?

Glad for any help!

softwarea
 
Posts: 8
Joined: Sun Apr 17, 2016 1:59 pm

Wed Apr 27, 2016 7:45 am

Hello,

Thanks for your feedback.
The code
Code: Select all
var searchString = @"\<(.*?)\>";
defined searchString as a string rather than a regular expression, so replaced placeholders unsuccessfully.
Please try the below solution:
Code: Select all
Regex searchString =new Regex(@"\<(.*?)\>");
var replaceString = string.Empty;
document.Replace(searchString,replaceString);


Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Wed Apr 27, 2016 9:42 am

Oh yes!! Stupid me. I was thinking I could provide a RegExpression as a simple string.

Your code works wonderful.

Thanks, Caroline!!

softwarea
 
Posts: 8
Joined: Sun Apr 17, 2016 1:59 pm

Thu Apr 28, 2016 1:14 am

You're welcome, glad to help you.

If there is any question, welcome get it back to us again.

Sincerely,
Caroline
E-iceblue support team
User avatar

caroline.zhang
 
Posts: 291
Joined: Mon Mar 07, 2016 9:22 am

Return to Spire.Doc