[英]Python pattern search return only inside quotes
在 output 方面需要帮助。 截至目前,它输出整行
import re
pattern = re.compile('Computer([0-9]|[1-9][0-9]|[1-9][0-9][0-9])Properties')
with open("Test.xml") as f:
for line in f:
if pattern.search(line):
print(line)
结果
<Computer0Properties name="BRSM">
</Computer0Properties>
<Computer1Properties name="4U-142">
</Computer1Properties>
我不想要引号或结果周围的任何东西
BRSM
4U-142
试过
print(re.findall(r"(\"[\w\s]+\")",line ))
它输出
['"BRSM"']
[]
[]
完全错过了第二个结果
import re
pattern = re.compile('Computer([0-9]|[1-9][0-9]|[1-9][0-9][0-9])Properties')
with open("Test.xml") as f:
for line in f:
if pattern.search(line):
print(line.split("\"", 4)[1], line.split("\"", 4)[3])
#BRSM 4U-142
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.