Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式:删除相邻的重复行_Python_Regex - Fatal编程技术网

Python 正则表达式:删除相邻的重复行

Python 正则表达式:删除相邻的重复行,python,regex,Python,Regex,我从Youtube上提取了一些cc,我被下面这样的值所困扰,我不知道如何处理它。我擅长替换字符串和其他东西,但当事情变得严重时,我真的很糟糕:( 这个 应替换为: we all have a unique perspective on the world around us and believe it or not 使用这个正则表达式,你可以去掉所有只有一个单词的行,如果有行有多个单词并且完全重复,它们将被替换为只有一行 \w+\s*\n|([\w ]+)\n*(\1\n+)* 这里,交替

我从Youtube上提取了一些cc,我被下面这样的值所困扰,我不知道如何处理它。我擅长替换字符串和其他东西,但当事情变得严重时,我真的很糟糕:(

这个

应替换为:

we all have a unique perspective on the
world around us and believe it or not

使用这个正则表达式,你可以去掉所有只有一个单词的行,如果有行有多个单词并且完全重复,它们将被替换为只有一行

\w+\s*\n|([\w ]+)\n*(\1\n+)*
这里,交替
\w+\s*\n
中的第一部分匹配单个字行,并用空字符串替换,第二个交替
([\w]+)\n*(\1\n+)*
捕获组1中的一行,然后
(\1\n+)*
消耗任何重复的行,最后被重复多次的同一行组2替换


为什么需要使用正则表达式?您使用的是什么语言?我使用的是Python
sed-n-e'/^$/{x;p;d;}'-e x file.txt
看起来可能有用(只打印空行之前的行)请添加适当的语言标记。另外,请阅读抱歉,但目前我正在使用notepad++find/replace regex。在我尝试将其集成到Python代码之前,我想让它在本地工作。
\w+\s*\n|([\w ]+)\n*(\1\n+)*