Search code examples
pythonstringstring-matching

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.

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"))