How to test characters of a string

Christian Gollwitzer auriocus at gmx.de
Thu Jun 9 08:29:26 EDT 2022


Am 08.06.22 um 19:57 schrieb De ongekruisigde:
> On 2022-06-08, 2QdxY4RzWzUUiLuE at potatochowder.com <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>> On 2022-06-09 at 04:15:46 +1000,
>> Chris Angelico <rosuav at gmail.com> wrote:
>>
>> If you insist:
>>
>>      >>> s = 'nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin'
>>      >>> print(s.split(':'))
>>      ['nm-iodine', 'x', '996', '57', '', '/var/empty', '/run/current-system/sw/bin/nologin']
>>
>> Hesitantly, because this is the Python mailing list, I claim (a) ':' is
>> simpler than r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$', and
>> (b) string.split covers pretty much the same edge cases as re.search.
> 
> Ah, but you don't catch the be numeric of fields (0-based) 2 and 3! But
> agreed, it's not the best of examples.


Yes, that is a simplistic example, since the : can't even be quoted to 
appear in that format (which would require higher-order parsing than a 
simple split)

Fortunately, the OP has just given another requirement to recognise 
different patterns, and now it went into a direction where it will 
become quite ugly if you avoid REs or any other pattern matching tools.

This is also the main reason I suggested REs initially - often if you 
see other patterns in the data, you can easily adapt a RE solution, 
whereas you'll have to write the thing from ground up anew if you do it 
manually.

	Christian



More information about the Python-list mailing list