python регулярное выражение, получая несколько строк, которые начинаются и заканчиваются - PullRequest
0 голосов
/ 26 марта 2020

Привет, я новичок в python, и мне нужно извлечь данные с шаблоном

что мой ввод это

block_id: Fzu text: {'type': 'mrkdwn', 'text': 'start this is the line of
text i want to extract \n <www.test.nl|René Glaudemans> \n also
this end.', 'verbatim': False} type: section block_id: y0Z elements:
[{'type': 'button', 'action_id': 'yMLUy', 'text': {'type':
'plain_text', 'text': 'Bevestig', 'emoji': False}, 'style': 'primary',
'value': 'bevestigd'}] type: actions

Требуемый вывод:

this is the line of text I want to extract \n <www.test.nl|René Glaudemans> \n also this.

Каждый раз, когда требуемый вывод начинается и заканчивается словом «start» и заканчивается «end»

Я действительно не знаю, что делать

Ответы [ 2 ]

0 голосов
/ 26 марта 2020
import re
pattern = "start(.*\n.*\n.*[\n].*).\',"
txt = """block_id: Fzu text: {'type': 'mrkdwn', 'text': 'start this is the line of text i want to extract \n <www.test.nl|René Glaudemans> \n also
this end.', 'verbatim': False} type: section block_id: y0Z elements:
[{'type': 'button', 'action_id': 'yMLUy', 'text': {'type':
'plain_text', 'text': 'Bevestig', 'emoji': False}, 'style': 'primary',
'value': 'bevestigd'}] type: actions"""

x = re.findall(pattern, txt)
x = repr(''.join(x))
x = ' '.join(x.rsplit('\\n', 1))
print(x)

вывод:

this is the line of text i want to extract \n <www.test.nl|René Glaudemans> \n also this end
0 голосов
/ 26 марта 2020

Попробуйте следующий код, он будет работать с первого start до последнего end:

import re

regex = r"start\s(.+)\send"

strng = '''block_id: Fzu text: {'type': 'mrkdwn', 'text': 'start this is the line of
text i want to extract \n <www.test.nl|René Glaudemans> \n also
this end.', 'verbatim': False} type: section block_id: y0Z elements:
[{'type': 'button', 'action_id': 'yMLUy', 'text': {'type':
'plain_text', 'text': 'Bevestig', 'emoji': False}, 'style': 'primary',
'value': 'bevestigd'}] type: actions'''

s = re.search(regex, strng, re.MULTILINE | re.DOTALL).group(1)

print(s)

'this is the line of\ntext i want to extract \n <www.test.nl|René Glaudemans> \n also\nthis'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...