python - 如何使用正则表达式查找行并删除任何前面的行

标签 python regex

我正在寻找方法来删除多行字符串中包含特定字符串的行之前的每一行,如下所示:

string1 
string2
string3
==== bump
string4
string5
string6
==== bump

但只有第一个匹配的...

最后我想将其作为输出:

==== bump
string4
string5
string6
==== bump

最佳答案

import re
text = '''\
string1 
string2
string3
==== bump
string4
string5
string6
==== bump'''

print(re.split(r'(=== bump)', text, maxsplit=1)[-1])

产量

string4
string5
string6
==== bump

关于python - 如何使用正则表达式查找行并删除任何前面的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17216749/

相关文章:

python - Julia 的 `@edit` 宏的 Python 等价物是什么?

python - Pyspark 显示每行具有最低值的列

python - Keras BatchNormalization,究竟什么是样本标准化?

regex - 如何使用 bash 脚本验证电子邮件 ID?

python - 在 Python 中使用 glob.glob 和正则表达式与 unicode 文件名的文件系统独立方式

regex - 递归列出包含 m of n 正则表达式的文件

python - module._init_() 最多接受 2 个参数(给定 3 个)(scrapy 教程 w/xpath)

regex - 创建 React App 找不到测试

Java - 获取脚本标签内的文本

python - 基维 : how to get widget by id (without kv)