Home > OS >  Python parsing C file - finding function starting with xxx_(...)
Python parsing C file - finding function starting with xxx_(...)

Time:09-18

I'm trying to parse a c source file, and find all functions that match the following template:

umsg_xxxxx_yyyy_zzz(aaa,bbb,ccc,ddd)

examples:

umsg_test_uints_subscribe(1,1)
umsg_sensors_imu_receive(sub,&msg,portMAX_DELAY)
umsg_sensors_imu_publish(&imu_data);
umsg_sensors_imu_peek(&peek_msg);

I've tried something like this:

f = open(file)
print(file)

# find words starting with umsg_ and ending with )
for line in f:
    if re.search(r'umsg_[a-z] \(.*\)', line):
        print(line)

But it fails to find anything.

Could anyone help me figure out the regex expression?

Thanks!

CodePudding user response:

You might use a pattern matching 1 or more word characters that can also match an underscore, and a negated character class to match from (...)

umsg_\w \([^()]*\)

Regex demo

Or another variation starting with a word boundary, and matching word characters without consecutive underscores:

\bumsg(?:_[^\W_] ) \([^()]*\)

Regex demo

  • Related