【发布时间】:2023-03-24 08:51:01
【问题描述】:
我有一个 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"
可以假设字符串中没有换行符。该解决方案应该适用于具有任意数量参数的方法。
【问题讨论】:
-
这可能不是正则表达式的工作。我不熟悉Objective-C,但我怀疑正确执行此操作需要能够匹配括号(或至少计算它们)。正则表达式不能做到这一点,或者至少,它们不能没有比 Python 的
re模块更强大的扩展。 -
如果字符串就像你的第一行(没有额外的空格),什么规则会告诉你字符串要在哪里被分割;即为什么在
"bundleId"和"uuid:"之间的空间上?