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

随声附和 提交于 2019-12-23 02:54:15

问题


I have a list of names and nicknames in the format firstname/surname/nickname, and I need to reformat it so that the nickname comes first on the line and is placed in quotation marks.

I've been able to put quotation marks around the nicknames using:

Find what: (\w+).$

Replace with: "\1"

This turns:

Timothy Burr Tim

Into:

Timothy Burr "Tim"

However, I'm not sure about whether it's possible to move the nickname to the beginning of each line. My goal is:

"Tim" Timothy Burr


回答1:


You could use a lazy quantifier:

(.*?)(\w+)$

And replace the line with "\2" \1, see a demo here on regex101.com.
You cannot use the dot-star as it would only capture m for the second group.

To treat unwanted whitespaces (which might occur in the first group and then at the end of the new string), you could come up with the following regex:

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

See a demo for this on regex101.com as well.




回答2:


You're close, you just need to capture the rest of the line and replace it after.

Search:

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

Replace:

"\2" \1


来源:https://stackoverflow.com/questions/35072599/notepad-can-you-use-regex-to-move-the-last-word-on-a-line-to-the-front

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!