2

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"

2
  • 2
    Why are you using regex to parse a date? Use NSSateFormatter instead. Commented Oct 23, 2012 at 3:33
  • Because I wasn't sure that the date was valid, and thought that I might run into problems trying to parse invalid dates. Commented Oct 23, 2012 at 3:49

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

Comments

2

Use NSDateFormatter. It looks like you're probably getting this date from a Rails web service? Try this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'Z'"];

NSDate *date = [dateFormatter dateFromString:opening_time];

NSDateComponents *components = [[NSCalendar currentCalendar] 
                          components:kCFCalendarUnitHour fromDate:date];

NSInteger hour = [components hour]

// Hour should now contain 10 with your sample date

If you want to get any other components, change your components parameter adding the flags for the components you want to extract. Something like this

NSDateComponents *components = [[NSCalendar currentCalendar] 
                          components:kCFCalendarUnitHour | kCFCalendarUnitMinute 
                            fromDate:date];

NSInteger hour = [components hour];
NSInteger minute = [components minute];

Comments

0

If all you really want is the 10:00 part of this fixed format string, why not avoid the regular expression and simply get the substring:

NSString *timeStr = [opening_time substringWithRange:NSMakeRange(11, 5)];

Of course you might consider parsing the string into an NSDate using an NSDateFormatter with the specified format. Make sure the date formatter's locale is set to en_US_POSIX since it is fixed format and not user focused.

The approach you take depends on what you will do with the time you extract. If you want to display it to the user then you want to use an NSDateFormatter so you can format it properly for the user's locale.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.