正则表达式用 MS Word 中的另一个字符串替换字符串?

标签 regex ms-word

任何人都可以用正则表达式帮我转:

filename_author


author_filename

我正在使用 MS Word 2003 并尝试使用 Word 的“查找和替换”来执行此操作。我试过 use wildcards功能,但没有任何运气。

我只能以编程方式完成吗?

最佳答案

这是正则表达式:

([^_]*)_(.*)



这是一个 C# 示例:
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        String test = "filename_author";
        String result = Regex.Replace(test, @"([^_]*)_(.*)", "$2_$1");
    }
}

这是一个 Python 示例:
from re import sub

test = "filename_author";
result = sub('([^_]*)_(.*)', r'\2_\1', test)

编辑:为了在 Microsoft Word 中使用通配符执行此操作,请将其用作搜索字符串:

(<*>)_(<*>)



并替换为:

\2_\1



另请参阅 Add power to Word searches with regular expressions有关我在上面使用的语法的解释:

  • The asterisk (*) returns all the text in the word.
  • The less than and greater than symbols (< >) mark the start and end of each word, respectively. They ensure that the search returns a single word.
  • The parentheses and the space between them divide the words into distinct groups: (first word) (second word). The parentheses also indicate the order in which you want search to evaluate each expression.

关于正则表达式用 MS Word 中的另一个字符串替换字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/911784/

相关文章:

javascript - 如何在 'input' 上找到该值多次出现 '//'(正斜杠)的情况?

javascript - 我该如何做这个正则表达式?

linux - 使用 Applescript 或 Linux 从文件名中删除字符串

ms-access - 通过Access绑定(bind)Word文档需要什么引用资料?

asp.net - 通过 ASP.NET 替换 Word 文档中的文本

将单词和数字与空格和逗号匹配的 Javascript 正则表达式匹配模式

在 R 中使用正则表达式替换重复的字符串

javascript - 如何禁止 javascript 中的宽松日期?

excel - 从 Word 中复制并粘贴为图片命名的 Excel 范围

r - 带有图像的多个 Kable 表