Splitting a filename into words and numbers in Python
- by danspants
The following code splits a string into a list of words but does not include numbers:
txt="there_once was,a-monkey.called phillip?09.txt"
sep=re.compile(r"[\s\.,-_\?]+")
sep.split(txt)
['there', 'once', 'was', 'a', 'monkey', 'called', 'phillip', 'txt']
This code gives me words and numbers but still includes "_" as a valid character:
re.findall(r"\w+|\d+",txt)
['there_once', 'was', 'a', 'monkey', 'called', 'phillip', '09', 'txt']
What do I need to alter in either piece of code to end up with the desired result of:
['there', 'once', 'was', 'a', 'monkey', 'called', 'phillip', '09', 'txt']