We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Find the text between start and end tags and replace in a document template

Hi, we have a document template which has multiple option tags with text. The text inside the option tags along with the option tag will be deleted based on the condition. For example, in the attached template, we have 3 options. If the condition met the first option, the rest of the options text along with the tag should be deleted. We have tried the find and replace with regex options, but unable to get the text inside the tags. 

TextSelection[] textSelections1 = doc.FindAll(new Regex(@"<Option1>(.+?)</Option1>", RegexOptions.Multiline));

TextSelection[] textSelections2 = doc.FindAll(new Regex(@"<Option1>(.*)</Option1>", RegexOptions.Multiline));

Please help us how to perform this.

Attachment: POC_Sample_Document_fb7206d1.zip

1 Reply 1 reply marked as answer

SB Suriya Balamurugan Syncfusion Team March 8, 2023 05:21 PM UTC

Hi Bali,

From the given details, we have found that your requirement is to find the text inside the tags which is extended to several paragraphs using Regex.

To achieve your requirement, we suggest you to use the FindSingleLine API in the Word document to find the multiple paragraph text as per the below code snippet,

//Find the first occurrence of particular text extended to several paragraphs in the document.

TextSelection[] textSelections1 = document.FindSingleLine(new Regex(@"<Option1>(.*)</Option1>", RegexOptions.Multiline));

TextSelection[] textSelections2 = document.FindSingleLine(new Regex(@"<Option2>(.*)</Option2>", RegexOptions.Multiline));

TextSelection[] textSelections3 = document.FindSingleLine(new Regex(@"<Option3>(.*)</Option3>", RegexOptions.Multiline));

Refer our UG documentation to know more about FindSingleLine API,

Refer our GitHub sample from here.

Suriya Balamurugan.

Marked as answer
Live Chat Icon For mobile
Up arrow icon