Fork me on GitHub

正则表达式NSRegularExpression

NSString *taskTitleStyle = @"font-weight: bold; color: rgb(238, 236, 224); ";

NSError *error = NULL;
NSString *substringForFirstMatch;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b(\\d)*\\,\\s(\\d)*\\,\\s(\\d)*\\b"
options:NSRegularExpressionCaseInsensitive
error:&error];

NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:taskTitleStyle options:0 range:NSMakeRange(0, [taskTitleStyle length])];

if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
substringForFirstMatch = [taskTitleStyle substringWithRange:rangeOfFirstMatch];
NSLog(@"%@", substringForFirstMatch);//打印238, 236, 224
}

NSArray *array = [substringForFirstMatch componentsSeparatedByString:@", "];

return [UIColor colorWithRed:[[array objectAtIndex:0] floatValue] / 255 green:[[array objectAtIndex:1] floatValue] / 255 blue:[[array objectAtIndex:2] floatValue] / 255 alpha:1];

posted on 2012-03-02 19:39  pengyingh  阅读(1411)  评论(0)    收藏  举报

导航