我的行很长,有很多情况,这些行的第一个单词相同(空格前的第一个字符串),但是其余的则不同。我只需要保留一行带有唯一的第一个字符串。
john jane
john 123
john jim jane
jane john
jane 123
jane 456
jim
jim 1
要获得此结果:
john jane
jane john
jim
因此,如果行中的第一个单词是匹配项,则删除除一行外的所有单词。
我可以删除所有重复的行,但保留上面示例中的行,
^(.*)(\r?\n\1)+$
此正则表达式删除相同的行,与示例不同。如果有正则表达式或记事本宏来解决此问题?
使用Notepad ++ (假设具有相同第一个单词的行是连续的):
搜索:^(\S++).*\K(?:\R\1(?:\h.*|$))+
更换:没有
图案细节:
^ # start of the line
(\S++) # the first "word" (all that isn't a whitespace) captured in group 1
.* # all characters until the end of the line
\K # remove characters matched before from the match result
(?:
\R # a newline
\1 # reference to the capture group 1 (same first word)
(?:
\h.* # a horizontal whitespace
| # OR
$ # the end of the line
)
)+ # repeat one or more times
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句