Assume that I want to modify all patterns in a script, take one line as an example:
line = "assert Solution().oddEvenList(genNode([2,1,3,5,6,4,7])) == genNode([2,3,6,7,1,5,4]), 'Example 2'"
Notice that function genNode is taking List[int] as the parameter. What I want is to remove the List, and keep the all the integers in the list, so that the function is actually taking *nums as the parameters.
Expecting:
line = "assert Solution().oddEvenList(genNode(2,1,3,5,6,4,7)) == genNode(2,3,6,7,1,5,4), 'Example 2'"
I've come up with a re pattern
r"([g][e][n][N][o][d][e][(])([[][0-9\,\s]*[]])([)])"
but I am not sure how I could use this... I can't get re.sub to work as it requires me to replace with a fixed string.
How can I achieve my desired result?
genNodeinstead of using the character class notation[g][e][n][N][o][d][e]'['and']'gone in all casesprint(re.sub(r'(genNode\()\[([0-9,]+)](\))', r'\1\2\3', line))(genNode\()\[(\d+(?:,\d+)*)\](?=\))regex101.com/r/vaxDcU/2