[英]Print between string and a number through multiple lines
I'm trying to print the number 0123
which is located between a line of string and a line of a number 我正在尝试打印位于字符串行和数字行之间的数字
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)
My code prints the output like this ( "0" )
which is wrong,I want my output to be like this ( "0123" )
我的代码打印出这样的输出
( "0" )
,这是错误的,我希望我的输出像这样( "0123" )
You can try this. 你可以试试看
test Code:\\s*(.*?)(?=\\s+1)
"*" matches 0 or more ,"+" mathes 1 or more “ *”匹配0或更多,“ +”匹配1或更多
You could capture your value without using a positive lookahead by matching the newline and the whitespace characters after the group. 您可以通过在组后匹配换行符和空格字符来捕获您的价值,而无需使用积极的前瞻性。
test Code:\n\s*(\d+)\s*\n\s*1
If 1 should be between a line of string and an line of number only, you might use: 如果1仅应在字符串行和数字行之间,则可以使用:
^test Code:\n\s*(\d+)\s*\n\s*1\s*$
You could go for 你可以去
^
[^\d\n]+[\r\n] # line without digits
(\d+)[\r\n] # only digits in that line
\s+\d+ # whitespaces + digits
See a demo on regex101.com (mind the verbose and the multiline flag). 请参阅regex101.com上的演示 (注意详细信息和多行标志)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.