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

Split by '.' when not preceded by digit

I want to split '10.1 This is a sentence. Another sentence.'
as ['10.1 This is a sentence', 'Another sentence'] and split '10.1. This is a sentence. Another sentence.' as ['10.1. This is a sentence', 'Another sentence']

I have tried

s.split(r'\D.\D')

It doesn’t work, how can this be solved?

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

>Solution :

If you plan to split a string on a . char that is not preceded or followed with a digit, and that is not at the end of the string a splitting approach might work for you:

re.split(r'(?<!\d)\.(?!\d|$)', text)

See the regex demo.

If your strings can contain more special cases, you could use a more customizable extracting approach:

re.findall(r'(?:\d+(?:\.\d+)*\.?|[^.])+', text)

See this regex demo. Details:

  • (?:\d+(?:\.\d+)*\.?|[^.])+ – a non-capturing group that matches one or more occurrences of
    • \d+(?:\.\d+)*\.? – one or more digits (\d+), then zero or more sequences of . and one or more digits ((?:\.\d+)*) and then an optional . char (\.?)
    • | – or
    • [^.] – any char other than a . char.
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