Regular expression to match function name and all arguments in Python

前端 未结 3 995
囚心锁ツ
囚心锁ツ 2021-01-17 18:20

Suppose I have a string such as the following:

\"func(arg1, arg2, arg3, arg4, ..., argn)\"

EDIT: This function is not in some particular la

3条回答
  •  难免孤独
    2021-01-17 18:41

    Looks like you're 90% there, why not just swap the arg and args groupings and do:

    import re
    
    fn_match = re.match(r"(?P\w+)\s?\((?P(?P\w+(,\s?)?)+)\)", s)
    fn_dict = fn_match.groupdict()
    del fn_dict['args']
    fn_dict['arg'] = [arg.strip() for arg in fn_dict['arg'].split(',')]
    

提交回复
热议问题