14

我在使用 Python Regex 时遇到了一个小问题。

假设这是输入:

(zyx)bc

我想要实现的是获得括号之间的任何内容作为单个匹配,以及外部的任何字符作为单个匹配。期望的结果将是:

['zyx','b','c']

应保持比赛的顺序。

我已经尝试使用 Python 3.3 获得它,但似乎无法找出正确的正则表达式。到目前为止,我有:

matches = findall(r'\((.*?)\)|\w', '(zyx)bc')

print(matches)产生以下结果:

['zyx','','']

任何想法我做错了什么?

4

5 回答 5

18

从以下文档re.findall

如果模式中存在一个或多个组,则返回组列表;如果模式有多个组,这将是一个元组列表。

虽然您的正则表达式匹配字符串三次,但(.*?)后两次匹配的组为空。如果您想要正则表达式的另一半的输出,您可以添加第二组:

>>> re.findall(r'\((.*?)\)|(\w)', '(zyx)bc')
[('zyx', ''), ('', 'b'), ('', 'c')]

或者,您可以删除所有组以再次获取简单的字符串列表:

>>> re.findall(r'\(.*?\)|\w', '(zyx)bc')
['(zyx)', 'b', 'c']

不过,您需要手动删除括号。

于 2013-01-06T13:00:29.003 回答
6

其他答案向您展示了如何获得所需的结果,但需要手动删除括号的额外步骤。如果您在正则表达式中使用环视,则无需手动去除括号:

>>> import re
>>> s = '(zyx)bc'
>>> print (re.findall(r'(?<=\()\w+(?=\))|\w', s))
['zyx', 'b', 'c']

解释:

(?<=\() // lookbehind for left parenthesis
\w+     // all characters until:
(?=\))  // lookahead for right parenthesis
|       // OR
\w      // any character
于 2013-01-06T15:01:11.833 回答
3

让我们看看我们的输出使用re.DEBUG.

branch 
  literal 40 
  subpattern 1 
    min_repeat 0 65535 
      any None 
  literal 41 
or
  in 
    category category_word

哎呀,里面只有一个subpattern,但如果有一个,re.findall只会拉出subpatterns!

a = re.findall(r'\((.*?)\)|(.)', '(zyx)bc',re.DEBUG); a
[('zyx', ''), ('', 'b'), ('', 'c')]
branch 
  literal 40 
  subpattern 1 
    min_repeat 0 65535 
      any None 
  literal 41 
or
  subpattern 2 
    any None

更好的。:)

现在我们只需要把它变成你想要的格式。

[i[0] if i[0] != '' else i[1] for i in a]
['zyx', 'b', 'c']
于 2013-01-06T13:07:56.927 回答
2

文档特别提到了处理组,所以不要在带括号的模式周围放置一个组,你会得到一切,但你需要自己从匹配的数据中删除括号:

>>> re.findall(r'\(.+?\)|\w', '(zyx)bc')
['(zyx)', 'b', 'c']

或使用更多组,然后处理生成的元组以获取您寻找的字符串:

>>> [''.join(t) for t in re.findall(r'\((.+?)\)|(\w)', '(zyx)bc')]
>>> ['zyx', 'b', 'c']
于 2013-01-06T13:02:36.533 回答
1
In [108]: strs="(zyx)bc"

In [109]: re.findall(r"\(\w+\)|\w",strs)
Out[109]: ['(zyx)', 'b', 'c']

In [110]: [x.strip("()") for x in re.findall(r"\(\w+\)|\w",strs)]
Out[110]: ['zyx', 'b', 'c']
于 2013-01-06T13:05:17.697 回答