Problem splitting a string

Fredrik Lundh fredrik at pythonware.com
Sat Oct 15 07:16:05 EDT 2005


"SPE - Stani's Python Editor" wrote:

> Use re.split, as this is the fastest and cleanest way.
> However, iff you have to split a lot of strings, the best is:
>
> import re
> delimiters = re.compile('_| ')
>
> def split(x):
>   return delimiters.split(x)

or, shorter:

    import re
    split = re.compile('_| ').split

to quickly build a splitter for an arbitrary set of separator characters, use

    separators = "_ :+"

    split = re.compile("[" + re.escape(separators) + "]").split

to deal with arbitrary separators, you need to be a little bit more careful
when you prepare the pattern:

    separators = sep1, sep2, sep3, sep4, ...

    pattern = "|".join(re.escape(p) for p in reversed(sorted(separators)))
    split = re.compile(pattern).split

</F>






More information about the Python-list mailing list