Problem replacing text with Regex

Hello,

I followed the example ReplaceWithRegex but can’t seem to get Regex to replace text. I want to replace the paragraph with Regex as I don’t always know how the line will break. Could be a carriage return, or carriage return + line feed, etc. The Aspose sample works fine with words “[s|m]ad” to “bad” but once I enter an escape sequence it doesn’t seem work. Is there something I am overlooking? Does anyone know to how to properly do this? Using Aspose.Words v17.2.0.0.

Code:

var docFile = @"C:\Temp\Replace.doc";
var doc = new Aspose.Words.Document(docFile);
doc.Range.Replace(new Regex(@"This is the sample paragraph[\r\n|\r|\n|\v]I want to replace."), "This is my new paragraph.", new FindReplaceOptions());
doc.Save(docFile);

Thanks guys

Hi Warren,

Thanks for your inquiry. Please use the correct Regex in the code to fix this issue. Please use following code example to get the correct output.

Document doc = new Document(MyDir + "Replace.doc");
Regex rx = new Regex("This is the sample paragraph(.*)I want to replace.", RegexOptions.IgnoreCase | RegexOptions.Singleline);
doc.Range.Replace(rx, "This is my new paragraph.", new FindReplaceOptions());
doc.Save(MyDir + "17.2.0.docx");

Yep, that works! Thanks Tahir

Hi Warren,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Tahir,

Although the wildcard helps, there are situations where it can’t be used. Here’s an example.

The attached document (Fresh.docx) contains the following text:
"Blah blah blah


This is my first line
User1 signature
This is my first line
User2 signature
Blah blah blah
"

Let’s say I want to replace “This is my first line\rUser2 signature” with some text. I can’t use “This is my first line(.*?)User2 signature” as it will grab the User1 signature line. So I adjusted regex to do “This is my first line(\r\n|\r|\n|\v)User2 signature” but that doesn’t seam to work. (Currently on 17.3.0)

Here’s the code I am using:

var doc = new Aspose.Words.Document(req.DocumentFilePath);

if (doc.Range.Replace(new Regex("This is my first line(\r\n|\r|\n|\v)User2 signature"), RegexOptions.Singleline), "My replacement line", new FindReplaceOptions()) > 0)
{
    Console.WriteLine("Match found & replaced");
}

And here’s a working example: http://regexr.com/3fjbj

The other interesting part is that trying the .NET Regex.Match returns success so is this a bug?

Console.WriteLine($"Success1: {Regex.Match("Blah blah blah\rThis is my first line\rUser1 signature\rThis is my first line\rUser2 signature\rBlah blah blah\r", "This is my first line(\r\n|\r|\n|\v)User2 signature").Success}");

Console.WriteLine($"Success2: {Regex.Match("Blah blah blah\r\nThis is my first line\r\nUser1 signature\r\nThis is my first line\r\nUser2 signature\r\nBlah blah blah\r\n", "This is my first line(\r\n|\r|\n|\v)User2 signature").Success}");

Thanks

Hi Warren,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-15102. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.

You may workaround this issue by using following solution.

  1. Implement IReplacingCallback interface
  2. Find the first paragraph and insert the BookmarkStart node before it. Set the bookmark’s name e.g. bookmark.
  3. Find the second paragraph and insert the BookmarkEnd node after it.
  4. Set the bookmark’s text to empty string.
  5. Move the cursor to the bookmark and insert the desired content.

Please check the code example shared in following link. Hope this helps you.
Example 3: Use a Custom Evaluator

Thanks Tahir. Hopefully this bug gets resolved soon.

Hi Warren,

Thanks for your patience. It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-15102) as ‘Not a Bug’.

Following special meta-characters should be used for breaks:

&p is for paragraph break, &b for section break, &m - page break and &l - manual line break.

Please use following code example to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "Fresh.docx");
Regex regex = new Regex("This is my first line&pUser2 signature", RegexOptions.Multiline);
int match = doc.Range.Replace(regex, "My replacement line", new FindReplaceOptions());
Console.WriteLine(match);
doc.Save(MyDir + "17.3.0.docx");

Tahir,

Just tested it and seems to work just fine. Thank you!

Hi Warren,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.