![](/img/trans.png)
[英]can we search multiple pattern using etree findall() in xml?
[英]Using a variable in a findall search
好的,所以我从这样创建的字典(DoL)中读取变量:
for i in DoL.keys():
fobId = DoL["{}".format(i)]["FaceOuterBound"]
因此,可以说,在通过DoL进行的第一次迭代中:
fobId = 194
我试图创建一个包含变量fobId
的正则表达式findall表达式:
edgeloop_txt = re.findall(r'^#'+str(fobId)+r'.*#(\d+).*;', text)
为了找到以fobId
开头的text
行:
#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ;
最后提取数字#159
我的输出print(edgeloop_txt)
只给我空列表:
[]
[]
[]
...
编辑(提供MCVE):
示范文本:
...
#190 = DIRECTION ( 'NONE', ( -1.000000000000000000, -0.0000000000000000000, -0.0000000000000000000 ) ) ;
#191 = DATE_AND_TIME ( #277, #349 ) ;
#192 = ORIENTED_EDGE ( 'NONE', *, *, #253, .T. ) ;
#193 = CARTESIAN_POINT ( 'NONE', ( 75.00000000000000000, 35.00000000000000000, 0.0000000000000000000 ) ) ;
#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ;
#195 = ORIENTED_EDGE ( 'NONE', *, *, #205, .T. ) ;
#196 = CARTESIAN_POINT ( 'NONE', ( 0.0000000000000000000, 35.00000000000000000, 0.0000000000000000000 ) ) ;
...
我正在使用的代码:
for i in DoL.keys():
fobId = DoL["{}".format(i)]["FaceOuterBound"]
edgeloop_txt = re.findall(r'^#'+str(fobId)+r'.*#(\d+).*;', text)
print(edgeloop_txt)
其中DoL就是这样的字典:
{'Face0': {'AdvancedFace': 12, 'FaceOuterBound': 194, 'Plane': 326},
'Face1': {'AdvancedFace': 73, 'FaceOuterBound': 53, 'Plane': 230},
'Face2': {'AdvancedFace': 99, 'FaceOuterBound': 121, 'Plane': 123},
'Face3': {'AdvancedFace': 131, 'FaceOuterBound': 268, 'Plane': 270},
...
'Face9': {'AdvancedFace': 358, 'FaceOuterBound': 9, 'Plane': 363}}
好的,我想我已经解决了您的问题。 您正在使用大量文本,但是您的正则表达式中有一个^
指示行的开头。 问题是您没有遍历要立即解析整个文本的行,并且因为这样做是因为正则表达式的行首字符阻止您找到匹配项。
您可以选择逐行遍历文本,也可以从正则表达式中删除^
。
逐行迭代的示例:
for key,val in DoL.items():
fobId = val["FaceOuterBound"]
matches = []
for line in text.splitlines():
# split text into lines and compare to each line
response = re.match(r'^#'+str(fobId)+r'.*#(\d+).*;', line)
# if a match was found, store it in list of matches
if response: matches.append(response[1])
print(matches) # ['159']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.