Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'assume': 0.11; "skip:' 30": 0.15; 'appreciated!': 0.16; 'exists)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'key/value': 0.16; 'pairs': 0.16; 'python-list,': 0.16; "skip:' 60": 0.16; 'string': 0.17; 'wrote:': 0.17; 'trying': 0.21; 'parse': 0.22; 'posted': 0.22; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'separated': 0.29; "i'm": 0.29; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'nov': 0.35; 'received:209.85': 0.35; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'most': 0.61; 'effective': 0.63; 'more': 0.63; '20,': 0.65; 'opinions': 0.72; 'convinced': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=ph18SZswwJHfohYLHZzA9gAd2jzCsfZ4uGqTRQLkAY0=; b=Q+ykEP2qcKqrqo/JB4Hrdp8X/NscrBfWcg4vUmNZq4+Hvf38WpoB9Vy6zObwudKuuh z43J/LlxFF6ehNaXd24AaGeAUm4G6AJihlWAGZW5Ty/A2esAlJaOcIV3ogG4tnT/oFB4 IsUQfdqzD3ijFDFLQd+1B6kcM7QxuDZIJTVgaehyfQz/2F285WPhMQTocBFUIg6tOUOb wNKSrZKkmI0jvTGn1Axgpwnuv2cq9pVAZBFmVjBmcXfBlLWJa1NuQExCgCioAsm3KmBD ZwGKnoBeYLy3bwl8OmZ7rH6go/q02WKDF9ONIvTdj518pQM5tvCshjxrNQVRM31aJVAC l4/Q== MIME-Version: 1.0 In-Reply-To: References: Date: Tue, 20 Nov 2012 07:42:19 +1100 Subject: Re: Robust regex From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353357750 news.xs4all.nl 6911 [2001:888:2000:d::a6]:60831 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33553 On Tue, Nov 20, 2012 at 7:32 AM, Joseph L. Casale wrote: > Trying to robustly parse a string that will have key/value pairs separated > by three pipes, where each additional key/value (if more than one exists) > will be delineated by four more pipes. > > string = 'key_1|||value_1||||key_2|||value_2' > regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?' > > I am not convinced this is the most effective or safest, any opinions would > be greatly appreciated! Is regex a requirement? Since you posted this on python-list, I'm going to assume you're working in Python. string = 'key_1|||value_1||||key_2|||value_2' content = dict(map(lambda x: x.split("|||"),string.split("||||"))) --> {'key_1': 'value_1', 'key_2': 'value_2'} ChrisA