0
NSString *yourString = @"/Users/user/Downloads/data(1).txt.download/data(1).txt";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"/Users/user/Downloads/data(1).txt-*\\d*.download"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
// your code to handle matches here
NSLog(@"%@ %ld",match,flags);
}];
参考上面的代码,当有特殊字符"("
文件名,它不能匹配,因为正则表达式应该使用"\\("
着,当然我可以使用@"/Users/user/Downloads/data\\(1\\).txt-*\\d*.download"
匹配,但如何在文件名中包含其他特殊字符。请问有什么办法来处理这个场景的常用方法?如何不确定的字符传递,如“(”正则表达式?
参数regularExpressionWithPattern
应该是一个变量,[NSString stringWithFormat:xxx]
你可以删除 “(” 比较之前这样的特殊字符... – vaibhav
检查:http://stackoverflow.com/a/13430545/1308632 – Mayur