python - 如何在 python 中使用 re 模块匹配模式之间的行

标签 python regex

我有一个包含以下内容的字符串(多行):

"Name=My name
Address=......
\##### To extract from here ####
line 1
line 2
line 3
\##### To extract till here ####
close"

如何提取 "##### To extract *" 字符串之间的行,包括模式?

输出应如下所示:

\##### To extract from here ####
line 1
line 2
line 3

最佳答案

pat = re.compile('\\\\##### To extract from here ####'
                 '.*?'
                 '(?=\\\\##### To extract till here ####)',
                 re.DOTALL)

pat = re.compile(r'\\##### To extract from here ####'
                 '.*?'
                 r'(?=\\##### To extract till here ####)',
                 re.DOTALL)

关于python - 如何在 python 中使用 re 模块匹配模式之间的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5088821/

相关文章:

python - Numpy 数组 任意维度的顺序索引数组

regex - vim: 为什么空格字符在方括号表达式中不起作用,但\n却可以

regex - Lookahead 和 Look After 在 unix 终端中的 grep

python - Django MySQL 与 iRegex 搜索精确词匹配

python - 正则表达式匹配 MAC 地址(不同的分隔符、格式等)

python - 匹配 "without this"

python - 使用 Python 图像库进行高级裁剪

python - cxFreeze 使用 sc2reader 库和多处理破坏 Python 应用程序

python - 为什么 list.append() 返回 None?

php - 奇怪的 PHP 字符串错误