Python - Regex will find the second match first
I have a little problem with Python regex.
I need to find the function name on this line: ((number) arent on a line in my file)
(1)void f(int test);
(2)void h(int test);
(3)double f(int test1, int test2, ...);
(4)double f(int test1, int test2);
I have this code:
namePattern = "^[\s\S]*?\s?[*\s]*([a-zA-Z_][a-zA-Z_0-9]*)\s*\([\S\s]*?\).*?$"
functionName = re.sub(re.compile(namePattern, re.MULTILINE), r'\1', funcString)
when i print function name it prints function (3) f first , when i need to write (1) f first .
Can anyone help me figure out what regex will find function (1) f ? Thank.
BTW, I can't figure out why it finds the second function f first . Not the first, not the last, but the second. This is strange.
source to share
Use re.findall
with the following regex:
>>> s="""(1)void f(int test);
... (2)void h(int test);
... (3)double f(int test1, int test2, ...);
... (4)double f(int test1, int test2);"""
>>> re.findall(r'(\(\d\))[^( ]*(.*)\(',s)
[('(1)', ' f'), ('(2)', ' h'), ('(3)', ' f'), ('(4)', ' f')]
The regex r'(\(\d\))[^( ]*(.*)\('
contains two capturing groups: (\(\d\))
which will match the number in brackets, and the second one (.*)
which will match any thing before (
and after[^( ]*
source to share