简体   繁体   English

在findall搜索中使用变量

[英]Using a variable in a findall search

Ok so I am reading a variable from a dictionary (DoL) I have created as such: 好的,所以我从这样创建的字典(DoL)中读取变量:

for i in DoL.keys():
    fobId = DoL["{}".format(i)]["FaceOuterBound"]

So lets say, on the first iteration through DoL: 因此,可以说,在通过DoL进行的第一次迭代中:

fobId = 194

I have tried to create a regex findall expression which includes the variable fobId : 我试图创建一个包含变量fobId的正则表达式findall表达式:

edgeloop_txt = re.findall(r'^#'+str(fobId)+r'.*#(\d+).*;', text)

In order to find the line in text which begins with # fobId : 为了找到以fobId开头的text行:

#194 = FACE_OUTER_BOUND ( 'NONE', #159, .T. ) ;

And finally extract the number #159 最后提取数字#159

My output print(edgeloop_txt) just gives me empty lists: 我的输出print(edgeloop_txt)只给我空列表:

[]
[] 
[]
...

EDIT (providing an MCVE): 编辑(提供MCVE):

Sample text: 示范文本:

...
#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 ) ) ;
...

Code I am using: 我正在使用的代码:

for i in DoL.keys():
    fobId = DoL["{}".format(i)]["FaceOuterBound"]
    edgeloop_txt = re.findall(r'^#'+str(fobId)+r'.*#(\d+).*;', text)
    print(edgeloop_txt)

Where DoL is a dictionary as such: 其中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}}

Ok, so I think I've figured out your problem. 好的,我想我已经解决了您的问题。 You're using a large block of text but your regex has a ^ in it indicating beginning of line. 您正在使用大量文本,但是您的正则表达式中有一个^指示行的开头。 The problem is you're not iterating over lines you're parsing the entire text at once and because you're doing that your regex line start character is preventing you from finding your matches. 问题是您没有遍历要立即解析整个文本的行,并且因为这样做是因为正则表达式的行首字符阻止您找到匹配项。

Your options are to either iterate over your text line by line or simply remove the ^ from your regex. 您可以选择逐行遍历文本,也可以从正则表达式中删除^

Example of line by line iteration: 逐行迭代的示例:

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.

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