在Objective-C中从正则表达式中拉出字符串

Sea*_*ean 2 regex objective-c ios

我有一个字符串,2000-01-01T10:00:00Z 我想从该字符串中抽出时间:10:00

任何人都可以告诉我如何使用NSRegularExpression做到这一点

我尝试了以下代码,但它无法正常工作(没有返回结果)

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])];
Run Code Online (Sandbox Code Playgroud)

哪里opening_time"2000-01-01T10:00:00Z"

das*_*ght 9

我想你需要在你面前加倍斜杠\d:

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);
Run Code Online (Sandbox Code Playgroud)

这打印 10:00