I am trying to get a Python regex to search through a .c file and get the function(s) inside it.
For example:
int blahblah(
struct _reent *ptr __attribute__((unused)),
const char *old,
const char *new
)
{
...
I would want to get blahblah as the function.
This regex doesn't work for me, it keeps on giving me None: r"([a-zA-Z0-9]*)\s*\([^()]*\)\s*{"
(?<=(int\s)|(void\s)|(string\s)|(double\s)|(float\s)|(char\s)).*?(?=\s?\()
http://regexr.com?3332t
This should work for what you want. Just keep adding types that you need to catch.
re.findall(r'(?<=(?<=int\s)|(?<=void\s)|(?<=string\s)|(?<=double\s)|(?<=float\s)|(?<=char\s)).*?(?=\s?\()', string) will work for python.
The regular expression isn't catching it because of the parentheses in the arguments (specifically, the parentheses in __attribute__((unused))). You might be able to adapt the regular expression for this case, but in general, regular expressions cannot parse languages like C. You may want to use a full-fledged parser like pycparser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With