Home > OS >  python3 , get a block of string between tokens
python3 , get a block of string between tokens

Time:01-09

txt='''
-------
xxx: 111111111
     222222222
------------
yyy: 11111
     222222222
------------
zzz: 333
------------
'''

python, import re. how to get sub string between '--------' ? for example get

yyy: 11111
     222222222

CodePudding user response:

You can use:

import re

txt='''
-------
xxx: 111111111
     222222222
------------
yyy: 11111
     222222222
------------
zzz: 333
------------
'''

x  =  [x.strip() for x in re.split("[-]{1,}", txt) if x.strip()]

['xxx: 111111111\n     222222222', 'yyy: 11111\n     222222222', 'zzz: 333']

Demo

CodePudding user response:

txt='''
-------
xxx: 111111111
     222222222
------------
yyy: 11111
     222222222
------------
zzz: 333
------------
'''
import re
p=re.compile(r'yyy[\s\S]*?(?=----)')
m=p.search(txt)
print(m.group(0))
  •  Tags:  
  • Related