Aditya Kuls Aditya Kuls - 2 months ago 8
Python Question

How to split a string in python based on dots, ignoring decimal values?

I have the following string:


s = 'This .is sparta 1.2 version. Please check.'


I want to split it based on dots, while ignoring decimal figures.
So, Required Output :


['This ','is sparta 1.2 version','Please check']


I tried following:

re.split(r'\.(?!([\d+\.\d+]))',s)


The output I am getting is:


['This ', None, 'is sparta 1.2 version', '', ' Please check', None, '']


Why am I getting None and empty strings here? I just want the split strings, ie.


['This ','is sparta 1.2 version','Please check']


Please suggest the rectification here.

Answer

Try splitting on

(?<!\d)\.(?!\d)

It makes sure the dot isn't preceded, nor followed, by a digit.

See it here at regex101.

Comments