gpt4 book ai didi

正则表达式删除与第一个字符串匹配的行?

转载 作者:行者123 更新时间:2023-12-05 03:10:53 24 4
gpt4 key购买 nike

我有一长串包含很多情况的行,具有相同第一个单词(空格之前的第一个字符串)但其余部分不同的行。我只需要保留一行具有唯一的第一个字符串。

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.*|$))+
替换:无

demo

图案细节:

^             # 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

关于正则表达式删除与第一个字符串匹配的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38393614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com