[英]Print between string and a number through multiple lines
我正在尝试打印位于字符串行和数字行之间的数字0123
import re
s = """
test Code:
0123
1
text1 text2
"""
if re.findall(r'test Code:\s*(.*?)(?=\s*1)', s):
test=re.findall(r'test Code:\s*(.*?)(?=\s*1)', s)
print(test)
我的代码打印出这样的输出( "0" )
,这是错误的,我希望我的输出像这样( "0123" )
你可以试试看 test Code:\\s*(.*?)(?=\\s+1)
“ *”匹配0或更多,“ +”匹配1或更多
您可以通过在组后匹配换行符和空格字符来捕获您的价值,而无需使用积极的前瞻性。
test Code:\n\s*(\d+)\s*\n\s*1
如果1仅应在字符串行和数字行之间,则可以使用:
^test Code:\n\s*(\d+)\s*\n\s*1\s*$
你可以去
^
[^\d\n]+[\r\n] # line without digits
(\d+)[\r\n] # only digits in that line
\s+\d+ # whitespaces + digits
请参阅regex101.com上的演示 (注意详细信息和多行标志)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.