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

Free Support Forum - aspose.com

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)

What’s the problem? I use the latest version.
Thanks,
Shay.

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

Thanks,
Shay.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.range.replace_overload_2.html

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Your Regex contains paragraph break.

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

<o:p></o:p>

Hi Emillia,

Thanks for your request. I just answered your question here: <A href="https://forum.aspose.com/t/67656</A></P> <P>Best regards,</P>