问题
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