REPOST: Re: regex question: backreferences in brackets
Alex Martelli
aleax at aleax.it
Fri Dec 28 11:22:25 EST 2001
"Jeremy Jones" <cypher_dpg at yahoo.com> wrote in message
news:mailman.1009552952.16557.python-list at python.org...
...
"""
match_string = 'XXX|1|22|333|4444:'
test_compile = re.compile(r'XXX(.)[^|]{1}\1[^|]{2}\1[^|]{3}\1[^|]{4}(.)')
mymatch = test_compile.match(match_string)
if mymatch:
print "Found a match"
print mymatch.group(0)
else:
print "No match found"
"""
The format of the strings that I am trying to match are 3 specific
characters followed by some delimter followed by N number of characters
other than the delimiter followed by the delimiter, etc. The above code
snipped works and matches the string perfectly. I guess my question is
this: how can I match any other character except for the delimiter without
knowing it beforehand (which I won't know what it is beforehand)? I am
thinking use the backreference to the delimiter (i.e.
Simplest might be something like:
def clevermatch(match_string):
try: myre = r'XXX(.)[^|]{1}\1[^|]{2}\1[^|]{3}\1[^|]{4}(.)'.sub('|',
re.escape(match_string[3]))
except IndexError: return None
else return re.match(myre, match_string)
i.e., tweak the re pattern appropriately before using it as a re.
Alex
========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-milwwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!upp1.onvoy!onvoy.com!nntp1.phx1.gblx.net!nntp.gblx.net!nntp.gblx.net!enews.sgi.com!news-xfer.nuri.net!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: "Alex Martelli" <aleax at aleax.it>
Newsgroups: comp.lang.python
Subject: cmsg cancel <a0i67t$qi1$1 at serv1.iunet.it>
Control: cancel <a0i67t$qi1$1 at serv1.iunet.it>
Date: Mon, 31 Dec 2001 01:30:50 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.a0i67t$qi1$1 at serv1.iunet.it>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009775269 27193 211.57.49.2 (31 Dec 2001 05:07:49 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 05:07:49 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)
This message was cancelled from within Mozilla.
More information about the Python-list
mailing list