在 MS Word 中使用正则表达式替换文本 - C#

发布于 2024-08-06 22:06:49 字数 798 浏览 4 评论 0原文

我有一个 Word 文档,我想打开该文档并将所有社会安全号码实例替换为“测试”一词。

我已经有了打开该文档的代码。这是进行替换的代码。但是,此时我在使用正则表达式时遇到了问题:_wordApp.Selection.Find.Text = ; 在我的代码中。使用正则表达式是一个好方法还是有更好的方法?请记住,我必须匹配任何社会安全号码...因此: \b[0-9]{3}-[0-9]{2}-[0-9]{4}\b OR \b [0-9]{3}[0-9]{2}[0-9]{4}\b

提前致谢...

object replaceAll = Werd.WdReplace.wdReplaceAll;

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.Text = ;

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";

_wordApp.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                ref replaceAll, ref nullobj, ref nullobj, ref nullobj, ref nullobj);

I have a Word document that I want to open and replace all instances of a social security number with the word, "test".

I already have the code to open the document. Here's the code that would do the replacement. However, I'm having trouble with using regular expressions at this point: _wordApp.Selection.Find.Text = ; within my code. Is using regular expressions a good approach or is there a better approach? Bear in mind, I have to match any social security number... hence: \b[0-9]{3}-[0-9]{2}-[0-9]{4}\b OR \b[0-9]{3}[0-9]{2}[0-9]{4}\b

Thanks in advance...

object replaceAll = Werd.WdReplace.wdReplaceAll;

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.Text = ;

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";

_wordApp.Selection.Find.Execute(ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                ref replaceAll, ref nullobj, ref nullobj, ref nullobj, ref nullobj);

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

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

发布评论

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

评论(1

嘴硬脾气大 2024-08-13 22:06:49

MS Word 内置了通配符匹配功能。它不如正则表达式强大,但看起来能够匹配社会安全号码等简单模式。

(< 和 > 通配符匹配起始词和结束词边界。)

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.MatchWildcards = true;
_wordApp.Selection.Find.Text = "<[0-9]{3}-[0-9]{2}-[0-9]{4}>"; // SSN with dashes.

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.MatchWildcards = true;
_wordApp.Selection.Find.Text = "<[0-9]{9}>"; // SSN without dashes.

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";

MS Word has a wildcard matching capability built in. It's not as powerful as regular expressions, but it looks like it's able to match simple patterns like social security numbers.

(The < and > wildcards match start and end word boundaries.)

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.MatchWildcards = true;
_wordApp.Selection.Find.Text = "<[0-9]{3}-[0-9]{2}-[0-9]{4}>"; // SSN with dashes.

_wordApp.Selection.Find.Replacement.ClearFormatting();
_wordApp.Selection.Find.Replacement.Text = "test";

_wordApp.Selection.Find.ClearFormatting();
_wordApp.Selection.Find.MatchWildcards = true;
_wordApp.Selection.Find.Text = "<[0-9]{9}>"; // SSN without dashes.

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