Pull string from regex in Objective-C

ios, objective-c, regex

Solution

I think you need to double the slashes in front of your `\d`s:

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d{2}:\\d{2})" options:NSRegularExpressionCaseInsensitive error:NULL];
NSTextCheckingResult *newSearchString = [regex firstMatchInString:opening_time options:0 range:NSMakeRange(0, [opening_time length])];
NSString *substr = [opening_time substringWithRange:newSearchString.range];
NSLog(@"%@", substr);

This prints `10:00`

Problem

I have a string `2000-01-01T10:00:00Z` I want to pull time time out of that string: `10:00` Can anyone tell me how to do it using NSRegularExpression I tried the following code but it isn't working (returning no results) ``` NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\d{2}:\d{2})" options:NSRegularExpressionCaseInsensitive error:NULL]; NSString *newSearchString = [regex firstMatchInString:opening_time options:0 range:NSMakeRange(0, [opening_time length])]; ``` Where `opening_time` is `"2000-01-01T10:00:00Z"`

Original source