Last active
August 29, 2015 14:06
-
-
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>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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:®exError]; | |
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