Python使用正则表达式为此模式查找文本

时间:2014-05-03 18:13:51

标签: python regex

我想根据以下文字中出现db access

来分隔文字
db access alpha customer1
 deny bla bla
db access beta customer99
 permit bla bla
 permit bla bla
 permit bla bla
 permit bla bla
db access game customer14
 permit no
!

(实际文本以linux上的\r\n开头)。我想要

db access alpha customer1
 deny bla bla

db access beta customer99
 permit bla bla
 permit bla bla
 permit bla bla
 permit bla bla

db access game customer14
 permit no

作为单独的元素。我最接近的是,

>>> re.findall('db\s+access.*?db\s+access',txt,re.M|re.S)
['db access alpha customer1\n     deny bla bla\n    db access']

>>> re.findall('db\s+access.*?!',txt,re.M|re.S)
['db access alpha customer1\n     deny bla bla\n    db access beta customer99\n     permit bla bla\n     permit bla bla\n     permit bla bla\n     permit bla bla\n    db access game customer14\n     permit no\n    !']

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

如果你真的想要使用正则表达式,那么你可以使用积极的前瞻:

>>> txt = '''
... 
... db access alpha customer1
...  deny bla bla
... db access beta customer99
...  permit bla bla
...  permit bla bla
...  permit bla bla
...  permit bla bla
... db access game customer14
...  permit no
... !'''
>>> re.findall('db\s+access.*?(?=db\s+access|!)',s,re.M|re.S)
['db access alpha customer1\n deny bla bla\n', 'db access beta customer99\n permit bla bla\n permit bla bla\n permit bla bla\n permit bla bla\n', 'db access game customer14\n permit no\n']