Splitting a filename into words and numbers in Python

Posted by danspants on Stack Overflow See other posts from Stack Overflow or by danspants
Published on 2010-05-04T01:38:51Z Indexed on 2010/05/04 1:48 UTC
Read the original article Hit count: 482

Filed under:
|
|

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']

© Stack Overflow or respective owner

Related posts about python

Related posts about regex