1

好的,所以我正在从我创建的字典(DoL)中读取一个变量:

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

所以让我们说,在通过 DoL 的第一次迭代中:

fobId = 194

我试图创建一个包含变量的正则表达式 findall 表达式fobId

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

为了找到text以 # 开头的行fobId

#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}}
4

1 回答 1

0

好的,所以我想我已经解决了您的问题。您正在使用一大块文本,但您的正则表达式中有一个^表示行首的。问题是你没有迭代你正在一次解析整个文本的行,因为你这样做是因为你的正则表达式行开始字符阻止你找到你的匹配项。

您的选择是逐行迭代您的文本,或者只是^从您的正则表达式中删除。

逐行迭代示例:

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']
于 2017-12-13T17:06:14.150 回答