Free Support Forum - aspose.com

Remove Text Between Specific Strings

Hello Aspose Team,

I am new to Aspose words, I want to remove a specific para or lines for example.

Updated Question:

Answer: <map><m x1="232" x2="384" y1="116" y2="140" ss="0" a="0" /><m x1="319" x2="479" y1="210" y2="231" ss="0" a="0" /><m x1="246" x2="497" y1="363" y2="382" ss="0" a="0" /></map>

I want to remove all paragraphs with map codes from document. Just want to keep Answer: text.

I tried to remove using this code

doc.Range.Replace(new Regex(Regex.Escape("<map>")), new RemoveAddressLine(), false);
public class RemoveAddressLine : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
        Node currentNode = e.MatchNode;
        builder.MoveTo(currentNode);
        builder.CurrentParagraph.Remove();
        return ReplaceAction.Replace;
    }
}

but it also removed Answer: but I want to keep it.

I also tried to use Bookmarks

FindReplaceOptions replaceOptions = new FindReplaceOptions();
replaceOptions.ReplacingCallback = new BookMarkStartLine();
doc.Range.Replace(new Regex("<map>"), "", replaceOptions);
replaceOptions.ReplacingCallback = new BookMarkEndLine();
doc.Range.Replace(new Regex("</map>"), "", replaceOptions);
doc.Range.Bookmarks.Clear();

public class BookMarkStartLine : IReplacingCallback
{
   ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
   {
      DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
      Node currentNode = e.MatchNode;
      builder.MoveTo(currentNode);
      builder.StartBookmark("bookmark_start");
      return ReplaceAction.Replace;
   }
}

public class BookMarkEndLine : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
        Node currentNode = e.MatchNode;
        builder.MoveTo(currentNode);
        builder.EndBookmark("bookmark_end");
        return ReplaceAction.Replace;
    }
}

but it’s throwing an exception that can’t find bookmark_start in the docuement, Please help
Thanks in advance
Arbaz

@aadi1295

Thanks for your inquiry. You can use Node.Remove method to remove node from the document. The node you want to remove is of type Paragaph. We suggest you please read following article about document object model of Aspose.Words.
Aspose.Words Document Object Model

Could you please ZIP and attach your input and expected output Word documents here for our reference? We will then provide you more information about your query along with code.

Hello Tahir,
I updated the question, Please help me. Thanks

Update:
Got it working using simple Regex
doc.Range.Replace(new Regex(@"\<map\>(.*?)\</map\>"), "", replaceOptions);

Please remove this question, Thanks

@aadi1295

It is nice to hear from you that you have found the solution of your issue. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.