在 MS Word 中使用正则表达式替换文本 - C#
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MS Word 内置了通配符匹配功能。它不如正则表达式强大,但看起来能够匹配社会安全号码等简单模式。
(< 和 > 通配符匹配起始词和结束词边界。)
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.)