Skip to content

Instantly share code, notes, and snippets.

@planetexpress69
Last active August 29, 2015 14:06
Show Gist options
  • Save planetexpress69/7bf3d450d86491bfd51d to your computer and use it in GitHub Desktop.
Save planetexpress69/7bf3d450d86491bfd51d to your computer and use it in GitHub Desktop.
Improved extract - no line splitting required as regex can find multiple matches... returns a NSDictionary<NSString, NSNumber>
- (NSDictionary *)extract:(NSString *)sPayload
{
NSError *regexError = nil;
NSString *sPattern = @"MACRO_NET_WORK_TYPE_[0-9A-Z _]{1,}=[ ]{1,}'[0-9]{1,}'";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:sPattern
options:NSRegularExpressionCaseInsensitive
error:&regexError];
if (sPayload == nil) {
NSLog(@"Gimme some real food...");
return nil;
}
if (regexError) {
NSLog(@"Error initializing regex...");
return nil;
}
NSMutableDictionary *dResult = [NSMutableDictionary new];
NSArray *aMatches = [regex matchesInString:sPayload options:0 range:NSMakeRange(0, [sPayload length])];
for (NSTextCheckingResult *match in aMatches) {
NSString *sGotcha = [sPayload substringWithRange:[match rangeAtIndex:0]];
NSArray *aSplittedGotcha = [sGotcha componentsSeparatedByString:@"="];
[dResult setObject:[NSNumber numberWithInt:[[[aSplittedGotcha[1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] stringByReplacingOccurrencesOfString:@"'" withString:@""]intValue]]
forKey:[aSplittedGotcha[0]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}
return dResult.allKeys.count > 0 ? dResult : nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment