Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式组合了两个不同行的匹配信息_Python_Regex_Python 3.x_Python 2.7 - Fatal编程技术网

Python 正则表达式组合了两个不同行的匹配信息

Python 正则表达式组合了两个不同行的匹配信息,python,regex,python-3.x,python-2.7,Python,Regex,Python 3.x,Python 2.7,我想通过正则表达式从代码中提取4x4矩阵数据,以便在python中用于进一步处理 数据的名称(空间中点的三维位置的矩阵)与名称应关联的变换数据位于不同的行中。如果我理解正确,您可以使用组(…) name 'Point\d' 为要捕获的3D点创建匹配项。如何将变换数据链接到关联点 (?<=transform).+ 以下是一种提取所需数据的非常脆弱的方法: txt='''scenegraph { name 'Root' visible 1 ... ''' items = dic

我想通过正则表达式从代码中提取4x4矩阵数据,以便在python中用于进一步处理

数据的名称(空间中点的三维位置的矩阵)与名称应关联的变换数据位于不同的行中。如果我理解正确,您可以使用组(…)

name 'Point\d'
为要捕获的3D点创建匹配项。如何将变换数据链接到关联点

(?<=transform).+

以下是一种提取所需数据的非常脆弱的方法:

txt='''scenegraph {
  name 'Root'
  visible 1
  ...
'''
items = dict(m.groups() for i in txt.replace('\n','').split('scenegraph {')
    for m in [re.match(r".*\bname '([^']+)'.*\btransform ([0-9.e+\- ]+)",i)]
        if m is not None)
现在您有了一个包含
(名称,转换)
项的dict,您可以通过键迭代或访问项,例如:

for name, transform in items.items():
    print(name, transform.split())

# Root ['1.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '1.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '1.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '1.000000000e+00']
# Point1 ['8.133591413e-01', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '8.133591413e-01', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '8.133591413e-01', '0.000000000e+00', '9.277027100e-02', '6.371318848e+03', '4.141105652e+02', '1.000000000e+00']
# Point2 ['3.526667595e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '3.526667595e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '3.526667595e+00', '0.000000000e+00', '-4.233531475e+00', '6.362593262e+03', '4.306122437e+02', '1.000000000e+00']

您最好对手头的数据格式使用解析器(看起来像是JSON/Yaml的一些变体)。

正则表达式不能以健壮的方式处理这种情况。看起来很有希望,我明天会研究它!谢谢你迄今为止的帮助!不幸的是,我不知道解析器到底是什么,但我将深入了解它。您是否可以在不导入python模块的情况下使用解析,因为我可能会遇到无法导入像regex这样的模块的问题,因为代码必须在我们使用的程序中从python执行?
for name, transform in items.items():
    print(name, transform.split())

# Root ['1.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '1.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '1.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '1.000000000e+00']
# Point1 ['8.133591413e-01', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '8.133591413e-01', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '8.133591413e-01', '0.000000000e+00', '9.277027100e-02', '6.371318848e+03', '4.141105652e+02', '1.000000000e+00']
# Point2 ['3.526667595e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '3.526667595e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '0.000000000e+00', '3.526667595e+00', '0.000000000e+00', '-4.233531475e+00', '6.362593262e+03', '4.306122437e+02', '1.000000000e+00']