Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Question': 0.05; '(1,': 0.09; '0),': 0.09; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'output': 0.13; 'everyone,': 0.15; 'result.': 0.15; '4),': 0.16; 'expressions,': 0.16; 'only)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'wrote:': 0.16; 'string': 0.17; 'integer': 0.18; '>>>': 0.20; 'not,': 0.22; 'form:': 0.22; 'tuples': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'format,': 0.27; "skip:' 10": 0.28; 'regular': 0.29; 'dictionary': 0.29; 'spaces': 0.29; 'there!': 0.29; "i'd": 0.31; 'guess': 0.31; 'possibly': 0.32; 'problem': 0.33; 'surely': 0.33; 'values.': 0.33; 'functions.': 0.35; 'problem.': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'starting': 0.37; 'drop': 0.38; 'names': 0.38; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'between': 0.65; 'groups.': 0.72; 'received:12': 0.81 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Emile van Sebille Subject: Re: Question about regular expression Date: Wed, 30 Sep 2015 11:50:12 -0700 References: <811788b6-9955-4dcc-bf49-9647891d17ec@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: www.westernstatesglass.com User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: <811788b6-9955-4dcc-bf49-9647891d17ec@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443639036 news.xs4all.nl 23734 [2001:888:2000:d::a6]:40799 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97254 On 9/30/2015 11:34 AM, massi_srb@msn.com wrote: > Hi everyone, > > firstly the description of my problem. I have a string in the following form: > > s = "name1 name2(1) name3 name4 (1, 4) name5(2) ..." > > that is a string made up of groups in the form 'name' (letters only) plus possibly a tuple containing 1 or 2 integer values. Blanks can be placed between names and tuples or not, but they surely are placed beween two groups. I would like to process this string in order to get a dictionary like this: > > d = { > "name1":(0, 0), > "name2":(1, 0), > "name3":(0, 0), > "name4":(1, 4), > "name5":(2, 0), > } > > I guess this problem can be tackled with regular expressions, Stop there! :) I'd use string functions. If you can control the string output to drop the spaces and always output in namex(a,b)namey(c,d)... format, try starting with >>> "name1 name2(1) name3 name4(1,4) name5(2)".split() ['name1', 'name2(1)', 'name3', 'name4(1,4)', 'name5(2)'] then create the dict from the result. Emile