Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pull string from regex in Objective-C

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"

like image 940
Sean Avatar asked Apr 18 '26 23:04

Sean


1 Answers

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

like image 188
Sergey Kalinichenko Avatar answered Apr 20 '26 14:04

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!