简体   繁体   English

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

[英]Regex to replace string with another string in MS Word?

Can anyone help me with a regex to turn: 任何人都可以帮助我改变正则表达式:

filename_author

to

author_filename

I am using MS Word 2003 and am trying to do this with Word's Find-and-Replace. 我正在使用MS Word 2003,并尝试使用Word的查找和替换。 I've tried the use wildcards feature but haven't had any luck. 我尝试过use wildcards功能,但没有运气。

Am I only going to be able to do it programmatically? 我只能以编程方式进行吗?

Here is the regex: 这是正则表达式:

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

And here is a C# example: 这是一个C#示例:

using System;
using System.Text.RegularExpressions;

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

Here is a Python example: 这是一个Python示例:

from re import sub

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

Edit: In order to do this in Microsoft Word using wildcards use this as a search string: 编辑:为了在Microsoft Word中使用通配符执行此操作,请将其用作搜索字符串:

(<*>)_(<*>)

and replace with this: 并替换为:

\\2_\\1

Also, please see Add power to Word searches with regular expressions for an explanation of the syntax I have used above: 此外,请参阅使用正则表达式Word搜索添加电源,以获得我上面使用的语法的说明:

  • 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. 括号还指示您希望搜索评估每个表达式的顺序。

Here you go: 干得好:

s/^([a-zA-Z]+)_([a-zA-Z]+)$/\2_\1/

Depending on the context, that might be a little greedy. 根据具体情况,这可能有点贪心。

Search pattern: 搜索模式:

([^_]+)_(.+)

Replacement pattern: 替换模式:

$2_$1

In .NET you could use ([^_]+)_([^_]+) as the regex and then $2_$1 as the substitution pattern, for this very specific type of case. 在.NET中,您可以使用([^_]+)_([^_]+)作为正则表达式,然后使用$2_$1作为替换模式,对于这种非常特殊的情况。 If you need more than 2 parts it gets a lot more complicated. 如果你需要超过2个零件,它会变得更加复杂。

Since you're in MS Word, you might try a non-programming approach. 由于您使用的是MS Word,因此您可以尝试使用非编程方法。 Highlight all of the text, select Table -> Convert -> Text to Table. 突出显示所有文本,选择表格 - >转换 - >文本到表格。 Set the number of columns at 2. Choose Separate Text At, select the Other radio, and enter an _. 将列数设置为2.选择“分隔文本位”,选择“其他”单选按钮,然后输入_。 That will give you a table. 那会给你一张桌子。 Switch the two columns. 切换两列。 Then convert the table back to text using the _ again. 然后再次使用_将表格转换回文本。

Or you could copy the whole thing to Excel, construct a formula to split and rejoin the text and then copy and paste that back to Word. 或者您可以将整个内容复制到Excel,构建一个公式来拆分和重新加入文本,然后将其复制并粘贴回Word。 Either would work. 要么工作。

In C# you could also do something like this. 在C#中你也可以做这样的事情。

string[] parts = "filename_author".Split('_');
return parts[1] + "_" + parts[0];

You asked about regex of course, but this might be a good alternative. 你当然询问正则表达式,但这可能是一个很好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM