Jeffrey
Jeffrey

Reputation: 31

python regex getting a multiple lines that begin and end with

Hi I'm new to python and I need to extract data with a pattern

what my input is this

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

The wanted output:

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

Every time the wanted output starts and ends with the word "start" and ends with "end"

I really don't know what to do

Upvotes: 0

Views: 136

Answers (2)

Alok Singh
Alok Singh

Reputation: 116

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)

output:

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

Upvotes: 1

ManojK
ManojK

Reputation: 1640

Try the following code, it will work from first start to last 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'

Upvotes: 1

Related Questions