在python中使用re查找关键字旁边的两个字符串

时间:2015-11-21 00:32:16

标签: python

我需要在关键字旁边找到两个字符串。这是一个示例字符串

\plottwo{image1}{image2}

关键字是\ plottwo,正确的结果是

[image1, image2]

我知道如果关键字旁边只有一个字符串,我可以使用

re.findall('\plottwo.*?{(.*?)},text)

如何将其扩展为两个字符串?

1 个答案:

答案 0 :(得分:1)

请注意,这恰好匹配两个图像字符串:

import re

matcher = re.compile(r"""\\plottwo   # The literal \plottwo
                         {           # Opening brace for the first image group
                          (          # Start the first capture group
                           [^}]+     # Match anything OTHER than a closing brace
                          )          # End the first capture group
                         }           # Closing brace
                         {           # Opening brace for the second image group
                          (          # Start the second capture group
                           [^}]+     # Match anything OTHER than a closing brace
                          )          # End the second capture group
                         }           # Closing brace
                      """, re.VERBOSE)

print matcher.findall('\\plottwo{image1}{image2}')

如果您想捕获一个或两个图像字符串,请将其中一个捕获组设为可选:

import re

matcher = re.compile(r"""\\plottwo   # The literal \plottwo
                         {           # Opening brace for the first image group
                          (          # Start the first capture group
                           [^}]+     # Match anything OTHER than a closing brace
                          )          # End the first capture group
                         }           # Closing brace
                         (?:         # Non-saving group that we can make optional
                            {        # Opening brace for the second image group
                             (       # Start the second capture group
                              [^}]+  # Match anything OTHER than a closing brace
                             )       # End the second capture group
                            }        # Closing brace
                         )?          # End the non-capturing group
                      """, re.VERBOSE)

print matcher.findall('\\plottwo{image1}{image2}')
print matcher.findall('\\plottwo{image2}')

但要回应其中一条评论,正则表达式通常不是执行复杂解析作业的最佳方式(有时甚至是简单的解析作业: - )。