1

我试图找到如何从“ /games/usa/2013-05-01/game1.html ”中提取“ 2013-05-01/game1 ”,但我不知道该怎么做。我已经尝试了一些正则表达式,但它没有用。而且我也不知道这是否是最好的方法。这是我到目前为止所尝试的。

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/[0-9]{4}-[0-9]{2}-[0-9]-{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);

任何帮助,将不胜感激。

4

2 回答 2

4

一种方法是使用NSString'componentsSeparatedByString:方法。这是一个例子

NSString *str = [@"/games/usa/2013-05-01/game1.html" stringByDeletingPathExtension];
NSArray *components = [str componentsSeparatedByString:@"/"];
NSString *result = [NSString stringWithFormat:@"%@/%@", [components objectAtIndex:3], [components objectAtIndex:4]];
NSLog(@"%@", result);

当然,这将依赖于格式永远不会改变的事实,如果它确实改变,这很可能不会起作用。

于 2013-07-20T05:26:15.353 回答
2

尝试这个:

@"[0-9]{4}-[0-9]{2}-[0-9]{2}/[^.]+"

或这个:

@"[0-9]{4}-[0-9]{2}-[0-9]{2}/.+?(?=\\.html)"
于 2013-07-20T05:24:05.210 回答