string splitting

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Oct 16 16:27:00 EDT 2006


A pair of solutions:

>>> s = "central_african_republic_province.txt"
>>> s.rsplit("_", 1)[-1].split(".")[0]
'province'
>>> import re
>>> p = re.compile(r"_ ([^_]+) \.", re.VERBOSE)
>>> s = """\
... wisconsin_state.txt
... french_guiana_district.txt
... central_african_republic_province.txt"""
>>> p.findall(s)
['state', 'district', 'province']

Bye,
bearophile




More information about the Python-list mailing list