aspose.words-查找并替换功能 - 用新的line字符查找文本

发布于 2025-02-05 11:34:35 字数 2065 浏览 1 评论 0原文

我正在尝试使用Aspose.words(版本22.4)在Word文档中搜索和替换以下文本。但是,Aspose替换函数无法在文档中找到并替换文本。

[服务等待期:

[[[30天]主动连续服务。]

我正在使用以下代码,

rule.MergeField = "[SERVICE WAITING PERIOD:\r\n[[30 days] of Active continuous service.]]";
mergedDocument.Range.Replace(rule.MergeField.Replace("\n", "&l").Replace("\r", "&p"),
"abc", new Aspose.Words.Replacing.FindReplaceOptions()
{ MatchCase = false, FindWholeWordsOnly = false });

我还尝试替换\ n\ r在带有Aspose控制字符的文字中,没有任何运气。 有没有人尝试过这样的事情,或者迅速支持这种搜索文本?

感谢您的帮助。


06/09更新

用Alexey建议使用& p替换\ r \ n。太感谢了。需要在元理论中提供帮助,以使下面的文本迅速迅速。

\ r

示例文本 - 损失日期的年龄\原始福利金额\ r [年龄70-74] \ r [65%] \ r [年龄75-- 79] \ r [45%] \ r [年龄80-84] \ r [30%] \ r [年龄85岁或以上] \ r [15%] \ r

单词等效 -

\ t(tab)

示例文本 - 覆盖\ tperCentage of \ tpercentage of \ r \ r \ n \ tfour tfour tfour \ t [100% ] \ r \ n \ tthree肢体\ t [75%] \ r \ n \ ttwo四肢\ t [66.67%] \ r \ n \ n \ tone limb \ t [50%] \ r \ r \ n

更新06/15

我试图用\ t搜索并替换文本,但它与以下代码不起作用。

 rule.MergeField = "Class II:\t\tWhile participating in game, please make sure to wear your helmets.";

 mergedDocument.Range.Replace(rule.MergeField.Replace("\r\n", "&p").Replace("\t", ControlChar.Tab),
 finalValue.Replace("\r\n", "&p").Replace("\t", "&l"), new Aspose.Words.Replacing.FindReplaceOptions()
{ MatchCase = false, FindWholeWordsOnly = false });

I am trying to search and replace the below text in a word document using Aspose.Words (Version 22.4). But, the Aspose replace function is not able to find and replace the text in the document.

[SERVICE WAITING PERIOD:

[[30 days] of Active continuous service.]]

I am using the below code, where

rule.MergeField = "[SERVICE WAITING PERIOD:\r\n[[30 days] of Active continuous service.]]";
mergedDocument.Range.Replace(rule.MergeField.Replace("\n", "&l").Replace("\r", "&p"),
"abc", new Aspose.Words.Replacing.FindReplaceOptions()
{ MatchCase = false, FindWholeWordsOnly = false });

I have also tried replacing the \n, \r in text with the Aspose Control Characters without any luck.
Has anyone tried something like this or does aspose support such search text?

Thank you for your help.


06/09 Updates

Replacing \r\n with &p as suggested by Alexey worked. thank you so much. Need help on the metacharacters for aspose for below text.

\r

Example text - Age at Date of Loss\rPercent of Original Benefit Amount\r[Age 70-74]\r[65%]\r[Age 75-79]\r[45%]\r[Age 80-84]\r[30%]\r[Age 85 or over]\r[15%]\r

Word equivalent -

enter image description here

\t (Tab)

Example text - COVERED LOSS OF USE OF\tPERCENTAGE OF COVERAGE AMOUNT\r\n\tFour Limbs\t[100%]\r\n\tThree Limbs\t[75%]\r\n\tTwo Limbs\t[66.67%]\r\n\tOne Limb\t[50%]\r\n

Word equivalent -
enter image description here

UPDATES 06/15

I tried to search and replace the text with \t but it does not work with below code.

 rule.MergeField = "Class II:\t\tWhile participating in game, please make sure to wear your helmets.";

 mergedDocument.Range.Replace(rule.MergeField.Replace("\r\n", "&p").Replace("\t", ControlChar.Tab),
 finalValue.Replace("\r\n", "&p").Replace("\t", "&l"), new Aspose.Words.Replacing.FindReplaceOptions()
{ MatchCase = false, FindWholeWordsOnly = false });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2025-02-12 11:34:35

您应该用& p metacharacter替换序列\ r \ n,以匹配document中的段落中断。例如,请参见以下代码:

Document doc = new Document(@"C:\Temp\in.docx");

string match = "[SERVICE WAITING PERIOD:\r\n[[30 days] of Active continuous service.]]";

// Replace \r\n sequence (which usualy represents paragraph break in text documents) with &p metacharacter
// to match paragraph break in MS Word document.
match = match.Replace("\r\n", "&p");

doc.Range.Replace(match, "abc", new FindReplaceOptions() { MatchCase = false, FindWholeWordsOnly = false });

doc.Save(@"C:\Temp\out.docx");

& l metacharacter代表软件间断,即\ v,您可以使用shift+ shift+ shift+ shift+输入

更新:

您可以在匹配字符串和替换中使用\ t字符。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert some content with tab character.
builder.Write("This\tis\ttext\twith\ttab\tcharacters.");
// Save an intermadiate result.
doc.Save(@"C:\Temp\interm.docx");

// Now replace all tabs with sequence of whitespaces.
doc.Range.Replace("\t", "    ");
// Save the result.
doc.Save(@"C:\Temp\out.docx");

您突出显示的另一个角色是细胞中断。您不能替换它。 MS Word中的表单元总是包含一个段落,突出显示的字符是单元格中最后一个段落的末尾。它不能删除。

You should replace a sequence \r\n with &p metacharacter to match paragraph break in the Document. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

string match = "[SERVICE WAITING PERIOD:\r\n[[30 days] of Active continuous service.]]";

// Replace \r\n sequence (which usualy represents paragraph break in text documents) with &p metacharacter
// to match paragraph break in MS Word document.
match = match.Replace("\r\n", "&p");

doc.Range.Replace(match, "abc", new FindReplaceOptions() { MatchCase = false, FindWholeWordsOnly = false });

doc.Save(@"C:\Temp\out.docx");

&l metacharacter represent a soft line break, which is \v, you can insert such break in MS Word using Shift+Enter

UPDATE:

You can use \t character in your match string and replacement.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert some content with tab character.
builder.Write("This\tis\ttext\twith\ttab\tcharacters.");
// Save an intermadiate result.
doc.Save(@"C:\Temp\interm.docx");

// Now replace all tabs with sequence of whitespaces.
doc.Range.Replace("\t", "    ");
// Save the result.
doc.Save(@"C:\Temp\out.docx");

The other character you have highlighted is cell break. You cannot replace it. Table cell in MS Word always contain a paragraph and the highlighted character is the end of the last paragraph in the cell. It cannot be removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文