1

i have the following text :

  1. The first Heading This is heading 1 data
  2. The second header : this is heading 2 data
  3. third header this is heading 3 data

So, i am trying to write a single regex. i know for a fact that to extract data between heading 1 and heading 2, the following regex will work

The first Heading(.*?)The second header

The above will give the text "This is heading 1 data". But, what i am trying to get is to look for all the heading's that is a regex, which will return a list as follows

["This is heading 1 data","This is heading 2 data","This is heading 3 data"]

What i had in mind was the following

The first Heading(.*?)The second header(.*?)third header (.*?)

But, i am not getting any data for the above regex. can anyone help me with the solution

10
  • Use the DOTALL mode as you have linebreaks (at least in your sample). Commented May 7, 2018 at 10:01
  • 2
    @Kalyan Regex is about matching patterns of text. What is the heading pattern? If you cannot define it, you can hardly devise a reliable regex pattern to match it. Commented May 7, 2018 at 10:08
  • 1
    There was a similar question today. See stackoverflow.com/a/50206391/3832970. Basically, you may use re.split(r'[\r\n]+(?:{})(?:\s*:)?\s*'.format('|'.join(my_heading_list)), s) Commented May 7, 2018 at 10:19
  • 1
    Does that work for you? Please post the list of headings you have. Or can there be arbitrary texts in between/before/after the headings? Commented May 7, 2018 at 10:50
  • 1
    Please compile the list of known headings, and use them with the code I suggested above. Commented May 7, 2018 at 11:20

1 Answer 1

1

This should do it:

import re

a = '''Heading 1 This is heading 1 data
Heading 2 This is heading 2 data
Heading 3 This is heading 3 data'''

print(re.findall('(?<=Heading \d\s)(.*)(?:Heading \d|$)?', a)))
#['This is heading 1 data', 'This is heading 2 data', 'This is heading 3 data']
Sign up to request clarification or add additional context in comments.

Comments

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.