快速简易封装歌词文件
月半夜小夜曲.lrc
[ti:月半夜小夜曲] [ar:李克勤] [by:TTPod] [00:01.48]月半小夜曲 [00:05.66]作词:向雪怀 [00:10.66]作曲:河合奈保子 [00:15.63]演唱:李克勤 [00:20.63] [00:24.56]仍然倚在失眠夜望天边星宿 [00:30.46]仍然听见小提琴如泣似诉再挑逗 [00:36.30]为何只剩一弯月留在我的天空 [00:42.92]这晚以后音讯隔绝 [00:48.29]人如天上的明月是不可拥有 [00:54.26]情如曲过只遗留无可挽救再分别 [01:00.15]为何只是失望填密我的空虚 [01:06.82]这晚夜没有吻别 [01:12.23]仍在说永久想不到是借口 [01:18.40]从未意会要分手 [01:27.42]但我的心每分每刻仍然被她占有 [01:34.25]她似这月儿仍然是不开口 [01:39.12]提琴独奏独奏着明月半倚深秋 [01:45.52]我的牵挂我的渴望 直至以后 [01:51.70] [02:21.15]仍然倚在失眠夜望天边星宿 [02:27.06]仍然听见小提琴如泣似诉再挑逗 [02:32.97]为何只剩一弯月留在我的天空 [02:39.59]这晚以后音讯隔绝 [02:45.02]人如天上的明月是不可拥有 [02:51.08]情如曲过只遗留无可挽救再分别 [02:56.92]为何只是失望填密我的空虚 [03:03.63]这晚夜没有吻别 [03:08.75]仍在说永久想不到是借口 [03:15.23]从未意会要分手 [03:23.95]但我的心每分每刻仍然被她占有 [03:30.60]她似这月儿仍然是不开口 [03:35.79]提琴独奏独奏着明月半倚深秋 [03:42.26]我的牵挂我的渴望 直至以后 [03:47.86]仍在说永久想不到是借口 [03:54.09]从未意会要分手 [04:02.76]但我的心每分每刻仍然被她占有 [04:09.88]她似这月儿仍然是不开口 [04:14.85]提琴独奏独奏着明月半倚深秋 [04:21.25]我的牵挂我的渴望 直至以后 [04:29.00] [00:00.001]>>>>>>>>>>>>>! [00:00.009] [99:00.000]
分解不用正则表达式,而用for循环折腾你就out了.block遍历以及forin方式都是设计模式中的迭代器模式,效率要比for循环高哦.
YYRegex.h + YYRegex.m
// // YYRegex.h // AnalysizeSongFile // // Created by yuanyuan on 27/6/14. // Copyright (c) 2014年 yuanyuan. All rights reserved. // #import <Foundation/Foundation.h> typedef void(^textCheckingResult)(NSTextCheckingResult *match,NSRange matchRang); @interface YYRegex : NSObject #pragma mark -可读属性 @property (nonatomic,strong)NSString *regexString; //正则字符串 @property (nonatomic,strong)NSString *sourceString; //数据源字符串 #pragma mark -只读属性 @property (nonatomic,readonly)NSUInteger numberMatches; //数量的匹配 #pragma mark -类方法 + (void)string:(NSString *)string pattern:(NSString *)pattern each:(textCheckingResult)block; + (NSString *)string:(NSString *)string pattern:(NSString *)pattern replaceTemplate:(NSString *)string; #pragma mark -实例方法 - (instancetype)initWithSourceString:(NSString *)sourceString regexString:(NSString *)regexString; - (void)startAnalyse; - (void)each:(textCheckingResult)block; - (NSString *)replaceWithTemplateString:(NSString *)string; @end
// // YYRegex.m // AnalysizeSongFile // // Created by yuanyuan on 27/6/14. // Copyright (c) 2014年 yuanyuan. All rights reserved. // #import "YYRegex.h" @interface YYRegex () @property (nonatomic,strong)NSArray *resultArray; @property (nonatomic,strong)NSRegularExpression *regex; @end @implementation YYRegex - (instancetype)init { return [self initWithSourceString:nil regexString:nil]; } - (instancetype)initWithSourceString:(NSString *)sourceString regexString:(NSString *)regexString { self = [super init]; if (self) { _sourceString = sourceString; _regexString = regexString; } return self; } - (void)startAnalyse { if ([_sourceString length] && [_regexString length]) { // 正则表达式 _regex = \ [NSRegularExpression regularExpressionWithPattern:_regexString options:NSRegularExpressionCaseInsensitive error:nil]; // 执行相关匹配操作 NSRange range = NSMakeRange(0, [_sourceString length]); _resultArray = [_regex matchesInString:_sourceString options:0 range:range]; // 获取到个数 _numberMatches = [_resultArray count]; } else { NSLog(@"亲,请设置需要分析的字符串以及正则表达式"); } } - (void)each:(textCheckingResult)block { if (_resultArray) { for (NSTextCheckingResult *match in _resultArray) { block(match, [match range]); } } } - (NSString *)replaceWithTemplateString:(NSString *)string { if (string) { return [_regex stringByReplacingMatchesInString:_sourceString options:0 range:NSMakeRange(0, [_sourceString length]) withTemplate:string]; } else { return nil; } } + (void)string:(NSString *)string pattern:(NSString *)pattern each:(textCheckingResult)block { if ([string length] && [pattern length]) { // 正则表达式 NSRegularExpression *regex = \ [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil]; // 执行相关匹配操作 NSRange range = NSMakeRange(0, [string length]); NSArray *matches = [regex matchesInString:string options:0 range:range]; // 匹配好了 for (NSTextCheckingResult *match in matches) { block(match, [match range]); } } } + (NSString *)string:(NSString *)string pattern:(NSString *)pattern replaceTemplate:(NSString *)templateString { if ([string length] && [pattern length]) { // 正则表达式 NSRegularExpression *regex = \ [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil]; return [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:templateString]; } else { return nil; } } @end
打印信息如下:
使用相关的controller读取歌词的代码如下:
//读取歌词 NSString *str = [NSString stringWithContentsOfFile:BUNDLE_PATH(@"月半小夜曲.lrc") encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",str); //将歌词根据换行符分割成数组 NSArray *strArray = [str componentsSeparatedByString:@"\n"]; //分解歌词 [strArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { //初始化一个可变字符串 __block NSMutableString *tmpStr = [[NSMutableString alloc] initWithString:obj]; //正则表达式匹配出【**:**.**】的位置 [YYRegex string:tmpStr pattern:@"\\[..:..\\...\\]" each:^(NSTextCheckingResult *match, NSRange matchRang) { //获取时间轴 NSString *time = [tmpStr substringWithRange:matchRang]; //获取歌词文本 [tmpStr replaceCharactersInRange:matchRang withString:@""]; //打印相关信息 NSLog(@"%@ -- > %@",time,tmpStr); }]; }];