python - 正则表达式:删除彼此相邻的重复行
问题描述
我已经从 Youtube 中提取了一些 cc 并且我坚持使用下面的值,我不知道如何处理它。我擅长替换字符串和其他东西,但是当事情变得严重时我真的很糟糕:(
这个
we
all
have
a
unique
perspective
on
the
we all have a unique perspective on the
we all have a unique perspective on the
world
around
us
and
believe
it
or
not
world around us and believe it or not
world around us and believe it or not
应替换为:
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+)*
捕获 group1 中的一行,然后(\1\n+)*
消耗任何重复的行,最后被 group2 替换,这是同一行重复多次。
推荐阅读
- jenkins - 詹金斯将“来自 SCM 的管道脚本”转换为“自由式项目”
- spring-boot - JPA Hibernate deleteAll(列表
实体)未按预期工作 - java - 如果记录已存在,如何更新或在 SYBASE 中插入新记录?
- wordpress - Elementor 没有加载我的主题的functions.php
- python-3.x - 在不知道完整文件夹路径的情况下将文件保存在以前的目录中 | Python
- c# - User32 API SendMessage 从 Win10 OS 中的打开文件对话框打开文件
- flutter - 如何在 Flutter 2.2.1 的 CheckboxListTile 中应用单选?
- php - 如何设置自定义 url 以在页面上加载图像
- twisted - 双绞线已收到
- ruby - 无法升级 ruby 以安装 shopify 客户端