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"
I think you need to double the slashes in front of your \ds:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With