Notepad++ - 你可以使用正则表达式将一行中的最后一个单词移到前面吗?

Notepad++ - can you use regex to move the last word on a line to the front?

我有一个格式为 firstname/surname/nickname 的姓名和昵称列表,我需要重新格式化它,以便昵称排在第一位并放在引号中。

我已经能够使用引号将昵称括起来:

Find what: (\w+).$

Replace with: ""

这轮:

Timothy Burr Tim

进入:

Timothy Burr "Tim"

但是,我不确定是否可以将昵称移到每行的开头。我的目标是:

"Tim" Timothy Burr

您可以使用 惰性量词:

(.*?)(\w+)$

并将该行替换为 "" ,参见 a demo here on regex101.com
您不能使用点星号,因为它只会为第二组捕获 m

要处理不需要的空格(可能出现在第一组中,然后出现在新字符串的末尾),您可以提出以下正则表达式:

(.*?)(?:\s*(\w+))$
# turns Timothy Burr Tim to Tim Timothy Burr (no trailing whitespaces anymore)

也可以在 regex101.com 上查看演示。

你已经接近了,你只需要捕获该行的其余部分并在之后替换它。

搜索:

/^(.*?)\s*(\w+)$/m

替换:

""