We're sorry Aspose doesn't work properply without JavaScript enabled.

# Problem with Range.Replace

Hi,

I try to run the following line on the attached document:

I load the document to a Document object and then I run:

string regex = "xxxSingleCustomFieldx[^\r\n]*xxx";
m_document.Range.Replace(new Regex(regex), String.Empty);


This throws the next exception (I also add a part of the stack trace, which looks weird with odd characters):

The match includes one or more special or break characters and cannot be replaced.
----------------------

at հ.㓈.㓔(ReplaceEvaluatorArgs ԉ)
at հ.㓈.ᦂ()
at Aspose.Words.Range.Replace(Regex pattern, String replacement)


Thanks,
Shay.

we’ve found that if we add a line break after the xxxSingleCustomFieldx-something-xxx the Replace method works…

Thanks,
Shay.

Hi

Thanks for your request. Unfortunately, you can include special characters like PageBreak, ParagraphBreak, ColumnBreak etc into a captured or replacement string. See the following link for more information.
https://reference.aspose.com/words/net/aspose.words/range/replace/
Best regards.

Please take a look at the attached file again - there is NO page break, paragraph break, column break or other break in the captured or replacement string… The strings are, indeed, enclosed inside columns but we intend to replace the strings only, without any column breaks or other breaks.

Hi

string regex = "xxxSingleCustomFieldx[^\r\n]*xxx";


Just remove special characters from your Regex and all will work fine.
Best regards.

But the regular expression “[^\r\n]” means everything except \r and \n…

Hi
Thanks for your request. I think you should use the following regex.

Document doc = new Document(@"Test060\in.doc");
string regex = @"xxxSingleCustomFieldx\w*xxx";
Regex myRegex = new Regex(regex);
doc.Range.Replace(myRegex, "New value");
doc.Save(@"Test060\out.doc");


Hope this helps.
Best regards.

Hi,
I ended up using the regex @“xxxSingleCustomFieldx[\w\s]*xxx” because I needed to support whitespaces as well (strings like xxxSingleCustomFieldxSome name herexxx).

Thanks for the help,
Shay.

Hi,

I have a similar problem.
I try to replace tempsStr string in my Section and tempsStr can contain any special caracter. I don’t know how to create my Regex expression.

Dim s As Section = CType(node, Section)
s.Range.Replace(tempsStr, String.Empty, False, False)


-----------------------------------

dim regex as string = ???
Dim myRegex as Regex = new Regex(regex)
s.Range.Replace(myRegex, String.Empty)


Thanks for the help,
Emilia

Hi Emillia,