How to split a string by space, comma, and period characters but leave numbers intact in Python

Splitting a string by space, comma, and period characters and leaving numbers intact will return a list of the split words and numbers from a sentence. For instance “1.0, is.a.number will return [“1.0”, “is”, “a”, “number”].

Solution for How to split a string by space, comma, and period characters but leave numbers intact in Python : You can use re.split() to split a string by space, comma, and period characters Call re.split(pattern, string) to split string based on pattern, where pattern is the regular expression “\s|(?<!\d)[,.](?!\d)” where “\s” means to split by whitespace, “(?<!\d)[,.]” means to only split by , or . if it is not preceded by a digit, and “[,.](?!\d)” means to only split by , or . if it is not followed by a digit.


how-to-split-a-string-by-space-comma-and-period-characters-but-leave-numbers-intact-in-python