Word .NET库组件Spire.Doc系列教程(52):在Word中使用正则表达式查找和替换文本

该文将介绍如何使用正则表达式查找和替换Word 文档中的文本。

*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,查看折扣价!想要获取更多福利的朋友可以咨询在线客服哦~


用于测试的Word源文档如图:

Word .NET库组件Spire.Doc系列教程(52):在Word中使用正则表达式查找和替换文本

C#

using Spire.Doc;using System.Text.RegularExpressions;using System.Drawing;namespace WordDemo{    class Program    {        static void Main(string[] args)        {            Document doc = new Document();            doc.LoadFromFile("Sample.docx");            //替换以#开头的字符            Regex regex = new Regex(@"#w+b");            doc.Replace(regex, "Spire.Doc");            //替换[]内的字符            Regex regex1 = new Regex(@"[[sS]*]");            doc.Replace(regex1, "Spire.Doc for .NET");            //保存文档            doc.SaveToFile("Result.docx", FileFormat.Docx2013);        }    }}

VB.NET

Imports Spire.DocImports System.Text.RegularExpressionsImports System.DrawingNamespace WordDemo     Class Program        Private Shared Sub Main(ByVal args() As String)            Dim doc As Document = New Document            doc.LoadFromFile("Sample.docx")            '替换以#开头的字符            Dim regex As Regex = New Regex("#w+b")            doc.Replace(regex, "Spire.Doc")            '替换[]内的字符            Dim regex1 As Regex = New Regex("[[sS]*]")            doc.Replace(regex1, "Spire.Doc for .NET")            '保存文档            doc.SaveToFile("Result.docx", FileFormat.Docx2013)        End Sub    End ClassEnd Namespace

效果图:

Word .NET库组件Spire.Doc系列教程(52):在Word中使用正则表达式查找和替换文本


是E-iceblue官方友好合作伙伴,如果您对spire.Doc感兴趣,可以联系在线客服了解具体授权价格和使用机制。
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年3月1日
下一篇 2021年3月1日

相关推荐

发表回复

登录后才能评论