正则表达式只匹配一个字符串。需要它匹配两个

时间:2016-08-29 23:40:42

标签: python regex

我有以下正则表达式

^(.+?)(\s+engine$|\s+ROW_FORMAT)

启用了忽略大小写。

这个问题是,它匹配"引擎"或" row_format"并且两者都不匹配(如last example所示)。我在这里缺少什么?

In [17]: st = 'this is my engine and row_format'
In [18]: match = re.match('^(.+?)(\s+engine$|\s+ROW_FORMAT)', st, re.I)

In [19]: match
Out[19]: <_sre.SRE_Match at 0x26c5030>

In [20]: match.group(1)
Out[20]: 'this is my'

In [21]: st = 'this is my row_format and engine'

In [22]: match = re.match('^(.+?)(\s+engine$|\s+ROW_FORMAT)', st, re.I)

In [23]: match.group(1)
Out[23]: 'this is my'

In [24]: match.group(2)
Out[24]: ' row_format'

In [25]: match.group(3)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-25-da7df187e689> in <module>()
----> 1 match.group(3)

IndexError: no such group

1 个答案:

答案 0 :(得分:3)