Am 08.06.22 um 19:57 schrieb De ongekruisigde:
On 2022-06-08, 2qdxy4rzwzuui...@potatochowder.com
<2qdxy4rzwzuui...@potatochowder.com> wrote:
On 2022-06-09 at 04:15:46 +1000,
Chris Angelico <ros...@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
--
https://mail.python.org/mailman/listinfo/python-list