Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to detect section and subsection using regex

I want to extract sections and subsections and corresponding contents from a passage. I have the following code using regex but the code can’t detect the subsections. How to resolve it?

import re

def extract_sections_and_content(text):
    pattern = r'(?P<section>^\d+(?:\.\d+)*(?:\s+\S+)?\s+[A-Za-z]+)\n(?P<content>(?:(?!\d+(?:\.\d+)*(?:\s+\S+)?\s+[A-Za-z]+|\n\d+(?:\.\d+)*(?:\s+\S+)?\s+[A-Za-z]+).)*)'
    matches = re.finditer(pattern, text, re.DOTALL | re.MULTILINE)
    section_content_pairs = [(re.sub(r'^\d+(?:\.\d+)*(?:\s+\S+)?\s+', '', match.group('section').strip()), match.group('content').strip()) for match in matches]
    return dict(section_content_pairs)

# Example usage:
text = """
1 Introduction
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2 Background
Praesent euismod, arcu quis fermentum pulvinar, urna ex euismod ex.
2.1.2 Introduction to Topic
Vestibulum nec lorem eu ligula faucibus cursus.
3 Conclusion
Sed sed malesuada magna, at dignissim quam.
"""

result = extract_sections_and_content(text)
print(result)

the result I get:

{'Introduction': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 
 'Background': 'Praesent euismod, arcu quis fermentum pulvinar, urna ex euismod ex.', 
 'Conclusion': 'Sed sed malesuada magna, at dignissim quam.'}

the result I want is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'Introduction': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
 'Background': 'Praesent euismod, arcu quis fermentum pulvinar, urna ex euismod ex.',
 'Introduction to Topic': 'Vestibulum nec lorem eu ligula faucibus cursus.',
 'Conclusion': 'Sed sed malesuada magna, at dignissim quam.'}

>Solution :

Your code is close, but there’s an issue with the regex pattern. The named capture group for the section is too restrictive, and the negative lookahead assertion is causing the pattern to not match the content of sections that have subsections.

Try this:

import re

text = """
1 Introduction
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
2 Background
Praesent euismod, arcu quis fermentum pulvinar, urna ex euismod ex.
2.1.2 Introduction to Topic
Vestibulum nec lorem eu ligula faucibus cursus.
3 Conclusion
Sed sed malesuada magna, at dignissim quam.
"""

pattern = r'(\d+(\.\d+)*)\s+(.*?)\n(.*?)(?=\d+(\.\d+)*\s+|$)'

matches = re.findall(pattern, text, re.DOTALL)

result = {}
for match in matches:
    section = match[2]
    content = match[3]
    result[section] = content.strip()

print(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading