`re.search`没有给出完整的匹配(Python)
下面是我的文件“test.txt”的一行: 我想获取`re.search`没有给出完整的匹配(Python),python,Python,下面是我的文件“test.txt”的一行: 我想获取(“name”:“10.105.206.87”},{“id”:“118e8d36-49ba-11e7-8c4e-fb1fe31bd90c”,“type”)下一行的部分是我的代码- f=open("test.txt","r") str1=f.read() str2=str1.split("Device") for string in str2: if "name" in string: reg='\"name.+,\
(“name”:“10.105.206.87”},{“id”:“118e8d36-49ba-11e7-8c4e-fb1fe31bd90c”,“type”)
下一行的部分是我的代码-
f=open("test.txt","r")
str1=f.read()
str2=str1.split("Device")
for string in str2:
if "name" in string:
reg='\"name.+,\"typ'
match=re.search(reg,string)
print (match)
它与预期输出的一半相匹配:
<_sre.SRE_Match object; span=(164, 237), match='"name":"10.105.206.87"},{"id":"118e8d36-49ba-11e7>
如果我传递了错误的正则表达式,请告诉我。模式是正确的。您正在尝试打印重新搜索的结果。请尝试仅提取匹配的文本,如下所示
reg='\"name.+,\"type\"'
match=re.search(reg,string)
print (match.group())
顺便说一句,我已经对模式进行了一些修改,以匹配
“type”
看起来像JSON。请尝试使用JSON
模块。
"name":"10.105.206.87"},{"id":"118e8d36-49ba-11e7-8c4e-fb1fe31bd90c","type"
reg='\"name.+,\"type\"'
match=re.search(reg,string)
print (match.group())