加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:39:19 所属栏目:百科 来源:网络整理
导读:任何人都可以帮助我改变正则表达式: filename_author 至 author_filename 我正在使用MS Word 2003,并尝试使用Word的查找和替换.我尝试过使用通配符功能,但没有运气. 我只能以编程方式进行吗? 解决方法 这是正则表达式: ([^_]*)_(.*) 这是一个C#示例: usi
任何人都可以帮助我改变正则表达式:

filename_author

author_filename

我正在使用MS Word 2003,并尝试使用Word的查找和替换.我尝试过使用通配符功能,但没有运气.

我只能以编程方式进行吗?

解决方法

这是正则表达式:

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

这是一个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读