我正在尝试使用正则表达式解析以下格式的字符串:
"Key" = "Value";
以下代码用于提取“key”和“value”:
NSString* pattern = @"([\"\"'])(?:(?=(\\\\?))\\2.)*?\\1";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:NULL];
NSRange matchRange = NSMakeRange(0, line.length);
NSTextCheckingResult *match = [regex firstMatchInString:line options:0 range:matchRange];
NSRange rangeKeyMatch = [match rangeAtIndex:0];
matchRange.location = rangeKeyMatch.length;
matchRange.length = line.length - rangeKeyMatch.length;
NSTextCheckingResult *match2 = [regex firstMatchInString:line options:0 range:matchRange];
NSRange rangeValueMatch = [match2 rangeAtIndex:0];
它看起来效率不高,并且没有将以下示例视为无效:
"key" = "value" = "something else";
有没有任何有效的方法来执行这种解析的解析?