我想根据以下文字中出现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 !']
任何建议都会有所帮助。
答案 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']