stupid perl question

BJörn Lindqvist bjourne at
Sat May 27 01:51:57 CEST 2006

> how can i split a string that contains white spaces and '_'
> any clue?

If the white spaces and the '_' should be applied equivalently on the
input and you can enumerate all white space characters, you could do
like this:

def split_helper(list, delims):
    if not delims:
        return list
    ch = delims[0]
    lst = []
    for item in list:
        lst += split_helper(item.split(ch), delims[1:])
    return lst

def split(str, delims):
    return split_helper([str], delims)

>>> split("foo_bar eh", "_ ")
['foo', 'bar', 'eh']

Though I bet someone will post a one-line solution in the next 30 minutes. :)

mvh Björn

More information about the Python-list mailing list