1

I am parsing a C code file using Python and I need to find all function calls, but not function definitions. For example:

txt = "foo(param1, bar1(foobar()), param2, !bar2(param3));"

should give me something like foo, bar1, foobar, bar2.

The regex I am using is "\w+\(" :

import re
x = re.findall("\w+\(", txt)

but then it will also match function definition like this:

text = "void foo(int param) {"

How do I modify my regex?

1 Answer 1

3

You might be able to get away with using a negative lookahead in the matching pattern which asserts that no { appears in the input line:

txt = "foo(param1, bar1(foobar()), param2, !bar2(param3));"
matches = re.findall(r'(?!.*\{)\b\w+(?=\()', txt)
print(matches)

This prints:

['foo', 'bar1', 'foobar', 'bar2']

But your second input has no matches:

txt = "void foo(int param) {"
matches = re.findall(r'(?!.*\{)\b\w+(?=\()', txt)
print(matches)

This prints:

[]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, i'm trying to decode your regex :) I don't get why this group (?!.*\{) stays in front but not behind.
@hydradon The negative lookahead (?!.*\{) says to, from the very beginning of the string, assert that { does not occur anywhere later in the string (i.e. from start to end). We don't want to put this assertion later, because then it might admit some false positives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.