[英]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.