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 match number followed by . and a space character?

Basically I have some text like this:

  1. first line
  2. second
  3. more lines
  4. bullet points

I separate these line by line so I can process them, but I want to be able to see if a line actually starts with a number then a . and then a space character.

So I can use this so split the line into 2 and process each of these separately. The number part with the . and space will be treated differently than the rest.

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

What’s the best way to do this in Python? I didn’t want to do a simple number check as characters because the numbers can be anything but likely less than 100.

>Solution :

The following should get you the two parts (number + full stop) and (everything after space) into two capture groups.

import re


def get_number_full_stop(input_string: str):
    res = re.search("^(\d+\.)\s(.+)", input_string)
    if res:
        return res.groups()
    else:
        return None


print(get_number_full_stop("1. hello"))
print(get_number_full_stop("1.hello"))
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