I want to write a regular expression in Python that matches functions with more than two arguments, such that the following expressions match:
function(1, 2, 3)
function(1, 2, 3, 4)
function(1, function(1, 2), 3)
function(function(function(1, 2), 2), 2, 3, 4)
but the following don't:
function(1, 2)
function(1, function(1, function(1, 2)))
function(1, function(function(1), 2))
My best attempt was the following expression, which only works for the cases without nested functions:
\w+\((?:.*,){2,}.*\)
What expression should I use instead?
For fun, you can recursively
sub/replace the right-most functions with a placeholder (e.g, a hyphen) and only in the end,countthe remaining arguments in each expression :Output :
From the comments :
Then, you can use boolean indexing with the same logic used above :
Output :