Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: de.alt.folklore.computer Subject: Re: COMAL Date: 22 May 2025 12:24:07 GMT Organization: Stefan Ram Lines: 56 Expires: 1 Jun 2026 11:59:58 GMT Message-ID: References: <1006u01$2vsuo$1@news1.tnib.de> <100en1o$3i1vf$1@news1.tnib.de> <1t682cb016i265761n3e8%sfroehli@Froehlich.Priv.at> <11t682e0e0ai7803en3e8%sfroehli@Froehlich.Priv.at> <100l59e$hbj$1@news.bawue.net> <100la2e$82ni$1@news1.tnib.de> <100lcfc$lcu$1@news.bawue.net> <100mkh3$atd7$1@news1.tnib.de> <100miku$9k5$1@news.bawue.net> <100mnod$b5b7$1@news1.tnib.de> <100mq18$guc$2@news.bawue.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de A+0tOct4alJiaLqr2eXn+QerETyEfRAq1xDI/RgOoTsEEm Cancel-Lock: sha1:Ji0J7/96Ae/Glz/XQZx2b8veEGc= sha256:G62HONx0iIsu/Wbh6ZOuXFPYvsFc/0a50U0L/3TedYE= X-Copyright: (C) Copyright 2025 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: de-DE-1901 Xref: csiph.com de.alt.folklore.computer:50138 "Peter J. Holzer" schrieb oder zitierte: >Aber man könnte sowas machen: >try: > d = re.match(...).groupdict() > # use dictionary d here >except AttributeError: > # no match Ja. Das dictionary d erlaubt recht lesbare Programmierung: pattern = re.compile(r''' ^\( "(?P[^"]*)"\s+ # First quoted string "(?P[^"]*)"\s+ # Second quoted string (?P\d+\S*)\s+ # First number-like field (?P\d+\S*)\s+ # Second number-like field (?P\d+\S*) # Third number-like field \)\s+ (?P.*) # Remaining string $ ''', re.VERBOSE) match = pattern.match(line) if match: values = match.groupdict() . Nun kann man Ausdrücke wie "values['num1']" verwenden, welche durch die Verwendung von Namen wie "num1" anstelle bloßer Zahlen gut lesbar sind. Wenn es einem nur um Kürze geht, könnte man einmalig import re M = re.match # abbreviation for re.match G = lambda m: m.groups() if m else (None,) * 5 # quick group extractor definieren und dann m = M(r'^\("(.*?)"\s+".*?"\s+(\d.*?)\s+(\d.*?)\s+(\d.*?)\)\s+(.*?)$', line) value1, value2, value3, value4, value5 = G(m) schreiben. Oder ohne "G": m = M(r'^\("(.*?)"\s+".*?"\s+(\d.*?)\s+(\d.*?)\s+(\d.*?)\)\s+(.*?)$', line) if m: for i, v in enumerate(m.groups(), 1): exec(f"value{i} = {repr(v)}") . In Python werden die üblichen Mittel der Sprache (Module, Aufrufe, r-Strings) eben konsistent auch für reguläre Ausdrücke eingesetzt und keine Spezialsyntax. Dies bedeutet, daß Benutzer- bibliotheken dieselben Mittel verwenden können wie die Sprache selber (etwa um eigene, gleichberechtigte re-Bibliotheken zu implementieren). Es ermöglicht es auch, mit den Mitteln von Python (wie oben) Abkürzungen zu definieren, falls man Wert darauf legt.