0

I want to separate the text based on occurrence of db access in the following text,

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
!

(Actual text start with \r\n on linux). I want

db access alpha customer1
 deny bla bla

and

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

and

db access game customer14
 permit no

as separate element. I closest I can get is,

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

and

>>> 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    !']

Any suggestion will be helpful.

5
  • This doesn't look like a good use case for regex. Commented May 3, 2014 at 18:15
  • This looks like a good use case the split function. Commented May 3, 2014 at 18:16
  • txt.split('\r\ndb access ') Commented May 3, 2014 at 18:19
  • @AndersonGreen @RobertHarvey Yes, Other option I have is to convert txt in list and then iterate over to find db access and create a separate txt Commented May 3, 2014 at 18:20
  • You can also split a string without removing its separators: stackoverflow.com/questions/7866128/… Commented May 3, 2014 at 18:21

1 Answer 1

1

If you really want to make use of regex, then you could use a positive lookahead:

>>> 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']
Sign up to request clarification or add additional context in comments.

3 Comments

it works like a charm. I didn't get ?=db\s+access. Will you please explain this?
I saw the doc,docs.python.org/2/library/re.html it explain very well thanks.
@Netro It'd match something followed by db\s+access or ! without making those a part of the match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.