I’m trying to parse a string with unknown number of elements with RegEx in Python. Here is the example:
>>>> import re
>>>> re.match("\=( A([0-9]+))*", "= A1 A2 A3 A4").groups()[1::2]
('4',)
I expect to have:
('1', '2', '3', '4',)
How can I get the expected result?
EDIT:
re.findall will not work for me. Let me make a better example:
I want to match the following string:
_func(cmd, param1, param2, param3, param4)_
I don’t know in advance the nummber of parameters. I expected to solve it using the following code:
>>> re.match("(\w+)\(cmd(, (\w+))*\)", "func(cmd, param1, param2, param3, param4)")
But this do not work, since groups ()* are not expanded to many items, but only last is used. Any ideas?
This returns a list, and in your example you showed a tuple. I presume a list will work for you, but of course you can always do:
The answer I just gave doesn’t actually check for the
=in the input string. If you need to do that, you can always use two patterns and two steps:EDIT: Code that handles your
func()example:When I ran the above code,
lstwas set to:['param1', 'param2', 'param3', 'param4']pat_recognize_argslooks for the literal stringfuncwith a literal((which is backslash-escaped in the pattern sorewon’t try to use it to start a match group), then the literal stringcmd, and then a match group that matches anything up to a literal)character; then the match group is closed with a)and a literal)is there to match the actual)that finishes the function call. After this pattern matches, the match object will have group 1 set to just the interesting arguments from the function call.So next we set
s = m.group(1)and then havere.findall()pull out the arguments for us.