Question about regular expression

Denis McMahon denismfmcmahon at gmail.com
Thu Oct 1 17:41:05 EDT 2015


On Thu, 01 Oct 2015 15:53:38 +0000, Rob Gaddi wrote:

> 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.'

I actually used 2 regexes:

wordpatt = re.compile('[a-zA-Z]+')

numpatt = re.compile('[0-9]+')

replace all '(', ',' and ')' in the string with spaces

split the string on space

create an empty dict d

process each thing in the split list setting d[word]=[0,0] for each word 
element (wordpatt.match(thing)) (a list because I want to be able to 
modify it)

setting d[word][n] = int(num) for each num element (numpatt.match(thing)) 
with n depending on whether it was the first or second num following the 
previous word

then:

d = {x:tuple(d[x]) for x in d}

to convert the lists in the new dic to tuples

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list