Skip to main content
 首页 » 编程设计

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

2025年05月04日10kevingrace

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

"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)