[Python-ideas] This seems like a wart to me...
Ron Adam
rrr at ronadam.com
Fri Dec 12 02:12:15 CET 2008
Adam Olsen wrote:
> On Thu, Dec 11, 2008 at 4:58 PM, Ron Adam <rrr at ronadam.com> wrote:
>> There doesn't seem to be an obvious way to split on different characters.
>>
>>
>> A new to python programmer might try:
>>
>>>>> '1 (123) 456-7890'.split(' ()-')
>> ['1 (123) 456-7890']
>>
>> Expecting: ['1', '123', '456', '7890']
>>
>>
>>>>> '1 (123) 456-7890'.split([' ', '(', ')', '-'])
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: expected a character buffer object
>
>>>> re.split('[ ()-]', '1 (123) 456-7890')
> ['1', '', '123', '', '456', '7890']
>>>> re.split('[ ()-]+', '1 (123) 456-7890')
> ['1', '123', '456', '7890']
>
> str.split() handles the simplest, most common cases. Let's not
> clutter it up with a bad[1] impersonation of regex.
>
>
> [1] And if you thought regex was ugly enough to begin with...
These examples was just what a "new" programmer might attempt. I have a
feeling that most new programmers do not attempt regular expressions ie..
the re module, until sometime after they have learned the basics of python.
Ron
More information about the Python-ideas
mailing list