0

I have a string expression like param1=123,param2=bbb

I would like to get a list of similar {'param1':'123','param2':'bb'}

Or at least ['param1=123','param2=bbb']

Unfortunately, the design gives the

re.match('^(\w+?=\w+?,?)+$','param1=123,param2=bbb').groups()

does not produce the desired result

of course - this is part of a larger expression, and I would like to get this result by using regexp

1
  • Comrades, thank you for your feedback, but unfortunately I did not get what he wanted. My task was to describe the expression of a regexp and a dictionary for further processing. A solution that offers you the problem splits into many sub-tasks and complicated program. Perhaps Mr. Ned Batchelder rights, and my problem has no simple solution to the beautiful. Commented Oct 14, 2011 at 20:37

5 Answers 5

3
>>> dict(re.findall(r'(\w+)=(\w+)','param1=123,param2=bbb'))
{'param2': 'bbb', 'param1': '123'}
Sign up to request clarification or add additional context in comments.

Comments

2

I'd suggest avoiding regexps and splitting on the delimiters. E.g. :

>>> sample = 'param1=123,param2=bbb'
>>> [ x.split('=',1) for x in sample.split(',') ]
[['param1', '123'], ['param2', 'bbb']]
>>> dict([ x.split('=',1) for x in sample.split(',') ])
{'param2': 'bbb', 'param1': '123'}

2 Comments

The original example - this is part of the regexp, it's much more complicated. I would like to place the entire analysis in regexp.
@Arty: That's a fair point, but I stand by my suggestion/recommendation: which is to avoid regexps where possible.
0

Regexes can only return strings. Each group in the pattern produces one string. You've only got one group in your pattern, so it can only return one string for that group. What you want isn't possible with a single match of a regex pattern.

Instead, you could use finditer to find a pattern many times in the string, but that breaks your requirement that this be part of a larger pattern.

Your only option is to match all the assignments as one string, then split on the commas afterward.

Comments

0

Your string looks very much like query string parameters. What about using Python's urlparse library? It won't work with commas as separators, but you could change them to semicolons.

params = 'param1=123,param2=bbb'
params2 = params.replace(',', ';')

import urlparse
urlparse.parse_qs(params2) => {'param2': ['bbb'], 'param1': ['123']}

Comments

0

For these answers, I assume you have a string with parameter name and parameter value pairs formatted just as in your example, like 'param1=value1,param2=value2,param3=value3"

This is a general regex that will parse out the pairs of parameter name (=) parameter value into groups for each match

(?<=^|,)([^=]*)=([^,]*)(?=,\s?)

If you want a string out like this {'param1':'123','param2':'bb'}, you can run this replacement regex:

match expression:       (?<=^|,)([^=]*)=([^,]*)(,?)
replace expression:     '\1':'\2'\3

... then encapsulate all of that in curly brackets { and }... feed that into an eval statement, and you have a dictionary. (I have NEVER programmed python, but...) I believe you could do the following:

inputString = "param1=value1,param2=value2,param3=value3"
myParamDictionary = eval('{' + re.sub("(?<=^|,)([^=]*)=([^,]*)(,?)", "'\1':'\2'\3", inputString)

...but I have NEVER programmed in Python... python's flexibility seems like there might be a better way...

If you simply want an array with the names and values (not identified except by their indexes being even or odd), you could use this expression in a re.findall(regex, subject) statement:

(?<=^|,)([^=]*)|(?<==)([^,]*)

...this either will match the part after a comma (,) but before an equals sign (=) or it will match the part after an equals sign but before a comma. It will match zero-length names and values., so that the indexes can represent the type of data. To match only names or values with at least one character, use + instead of * - doing so may cause the indexes to be misaligned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.