简体   繁体   English

Python 模式搜索仅返回引号内

[英]Python pattern search return only inside quotes

need help with the output.在 output 方面需要帮助。 As of now it outputs the entire line截至目前,它输出整行

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)

Result结果

          <Computer0Properties name="BRSM">

          </Computer0Properties>

          <Computer1Properties name="4U-142">

          </Computer1Properties>

What I want no quotes or anything around the results我不想要引号或结果周围的任何东西

BRSM
4U-142


Have tried试过

print(re.findall(r"(\"[\w\s]+\")",line ))

it outputs它输出

['"BRSM"']
[]
[]

completly missing the second result完全错过了第二个结果

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM