Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97254
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Subject | Re: Question about regular expression |
| Date | 2015-09-30 11:50 -0700 |
| References | <811788b6-9955-4dcc-bf49-9647891d17ec@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.274.1443639036.28679.python-list@python.org> (permalink) |
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)<space>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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Question about regular expression massi_srb@msn.com - 2015-09-30 11:34 -0700
Re: Question about regular expression Emile van Sebille <emile@fenx.com> - 2015-09-30 11:50 -0700
Re: Question about regular expression Tim Chase <python.list@tim.thechases.com> - 2015-09-30 14:20 -0500
Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-30 23:30 +0000
Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-02 18:25 +0000
Re: Question about regular expression Emile van Sebille <emile@fenx.com> - 2015-09-30 20:58 -0700
Re: Question about regular expression Tim Chase <python.list@tim.thechases.com> - 2015-10-01 07:39 -0500
Re: Question about regular expression Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-10-01 15:53 +0000
Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-01 21:41 +0000
Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-01 21:31 +0000
csiph-web