我知道必须有一个更好/更快的方法来完成所有这些 - 但我现在强迫解决方案现在有效。这可以更有效地完成吗?
Psuedo代码:
zoneNAME='R::BBQ (ZP)_|_R::Family Room (ZP)_|_R::Firepit (ZP)_|_R::Kitchen (ZP)_|_R::Living Room_|_R::Media Room (ZP)_|_R::Portable (ZP)_|_R::Spa (ZP)_|_S::BBQ (ZP)_|_S::Family Room (ZP)_|_S::Firepit (ZP)_|_S::Kitchen (ZP)_|_S::Media Room (ZP)_|_S::Portable (ZP)_|_S::Spa (ZP)_|_'
a = re.sub('_|_', '', zoneNAME)
a = a.split('S::', 1)[0]
a = re.sub('R::', '', a)
a = re.split('\|', a)
a = filter(None, a)
最终输出:
['BBQ (ZP)', 'Family Room (ZP)', 'Firepit (ZP)', 'Kitchen (ZP)', 'Living Room', 'Media Room (ZP)', 'Portable (ZP)', 'Spa (ZP)']
答案 0 :(得分:1)
你可以使用正面观察和正面观察(见documentation):
NSDictionary *parameter = [[NSDictionary alloc]init];
parameter = @{@"device-id": @"iOSDeveloper1234567432",@"system": @1,@"token": @"iOS-TestToken",@"mail_enabled": @"false",@"mail": @"NULL", @"movies": @[@"matrix", @"matrix2", @"matrix3"]};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:0 error:nil];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSString *stringkey;
stringkey = (@"k=");
stringkey = [stringkey stringByAppendingString:APIKEY];
NSString *strings;
NSString *myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
myString = [myString stringByAppendingString:(@"&k=")];
myString = [myString stringByAppendingString:APIKEY];
NSLog(@"%@",myString);
myString = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = [myString dataUsingEncoding:NSSymbolStringEncoding];
NSData *postData = [NSJSONSerialization dataWithJSONObject:parameter options:0 error:&error];
NSLog(@"%@",error.localizedDescription);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL parameters:myString progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
这将返回所有以> a = re.findall('(?<=R::).*?(?=_\|_)', zoneNAME)
# '(?<=R::)x' -- positive lookbehind: matches 'x' that is preceded by 'R::'
# 'x(?=_\|_)' -- positive lookahead: matches 'x' that is followed by '_|_'
# .*? matches a sequence of any characters non-greedily
> a
> ['BBQ (ZP)', 'Family Room (ZP)', 'Firepit (ZP)', 'Kitchen (ZP)', 'Living Room',
'Media Room (ZP)', 'Portable (ZP)', 'Spa (ZP)']
开头且后跟'R::'
的子字符串的列表,并且它非贪婪地匹配这些字符串,以便不匹配来自第一个{{1}的整个字符串。 1}}到最后'_|_'
。
答案 1 :(得分:0)
尝试使用内置函数的代码:
set( zoneNAME.replace('R::','').replace('S::','').split('_|_') )