用于解析ObjC方法声明的Regex或其他方法

2024-09-29 17:14:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表中的Objective-C方法:

"hideAlertWithBundleId:(NSString *)bundleId uuid:(NSString *)uuid"

但也可以这样写出来:

"hideAlertWithBundleId:   (NSString *)  bundleId         uuid: (NSString *) uuid"

我想摘录:

["hideAlertWithBundleId:(NSString *)bundleId", "uuid:(NSString *)uuid"]

我在使用纯Python和正则表达式实现这一点时遇到了困难,但我知道这是可能的

请记住,括号中的值可以是任何。因此,我可能还需要分析:

"findImageWithKey:(id)arg1 inGroup:(id)arg2 andInfo:(img*)arg3"

可以假定字符串中没有换行符。解决方案应该适用于具有任意数量参数的方法


Tags: 方法idimguuidarg3括号arg1arg2
1条回答
网友
1楼 · 发布于 2024-09-29 17:14:54

我对Objective-C不太熟悉,但如果我正确理解了这个问题,那么:

import re
for str in ["hideAlertWithBundleId:(NSString *)bundleId uuid:(NSString *)uuid",
    "hideAlertWithBundleId:   (NSString *)  bundleId         uuid: (NSString *) uuid",
    "-(BOOL)findImageWithKey:(id)arg1 inGroup:(id)arg2 andInfo:(img*)arg3"]:
    result = re.findall(r'((?:-\s*\(\w+\)\s*)?\w+)\s*:\s*(\(\w+\s*\*?\))\s*(\w+)', str)
    result2 = [i[0] + ":" + i[1] + i[2] for i in result]
    print(result2)

结果:

['hideAlertWithBundleId:(NSString *)bundleId', 'uuid:(NSString *)uuid']
['hideAlertWithBundleId:(NSString *)bundleId', 'uuid:(NSString *)uuid']
['-(BOOL)findImageWithKey:(id)arg1', 'inGroup:(id)arg2', 'andInfo:(img*)arg3']

相关问题 更多 >

    热门问题