Question about regular expression

Rob Gaddi rgaddi at technologyhighland.invalid
Thu Oct 1 11:53:38 EDT 2015


On Wed, 30 Sep 2015 11:34:04 -0700, massi_srb 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, but I have
> no idea bout how to use them in this case (I'm not a regexp guy). Can
> anyone give me a hint? any possible different approach is absolutely
> welcome.
> 
> Thanks in advance!

There's a quote for this.  'Some people, when confronted with a problem, 
think “I know, I'll use regular expressions.”  Now they have two 
problems.'  

That one's not always true, but any time you're debating a regex solution 
it should at least come to mind.  Lots of people have posted lots of pure 
Python solutions.  I will simply comment that using any of them will make 
you fundamentally happier as time goes on than trying to shoehorn a regex 
in.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list