I try to write a python regex, which matches the signature of simple python functions.
Like:
def _func1_(arg1, arg2):
I created this regex:
"def ([^\W\d]+\w*)(\(( *[^\W\d]+\w* *,? *)*\)):"
Unfortunately, this is not really good. In the argument list, spaces can be within variable names, and an unnecessary comma can be at the and of the argument list, in my regex. Can somebody help me out with the correct regex for this case? Thanks in advance!
As a matter of fact, I recently wrote a simple regex for function header comments (To automatically format my homework for a CS class). Here’s the gist of it:
For the parameters, I would forgo using re and instead use
str.split(',')on capture group1. There’s no need to make it more complicated than it has to be.