简体   繁体   中英

Python pattern search return only inside quotes

need help with the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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