正则匹配,字段名称和字段说明
NSString *regExpString1 = @"<tr><td class="confluenceTd">.*?</td><td class="confluenceTd">.*?</td></tr>"; NSString *regExpString2 = @"<tr><td colspan="1" class="confluenceTd">.*?</td><td colspan="1" class="confluenceTd">.*?</td></tr>";
完全代码
__block NSString *htmlStr = [NSString stringWithContentsOfFile:@"/Users/mac/Desktop/查看源.htm" encoding:NSUTF8StringEncoding error:NULL]; NSString *regExpString1 = @"<tr><td class="confluenceTd">.*?</td><td class="confluenceTd">.*?</td></tr>"; NSString *regExpString2 = @"<tr><td colspan="1" class="confluenceTd">.*?</td><td colspan="1" class="confluenceTd">.*?</td></tr>"; NSArray<NSString *> *regExpStrings = @[regExpString1, regExpString2]; NSMutableDictionary *dict = @{}.mutableCopy; [regExpStrings enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:obj options:NSRegularExpressionCaseInsensitive error:nil]; // 对str字符串进行匹配 NSArray *matches = [regular matchesInString:htmlStr options:0 range:NSMakeRange(0, htmlStr.length)]; // 遍历匹配后的每一条记录 for (NSTextCheckingResult *match in matches) { NSRange range = [match range]; NSString *mStr = [htmlStr substringWithRange:range]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<tr><td class="confluenceTd">" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"</td></tr>" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<pre><span style="color: rgb(106,135,89);">" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<br /></span></pre>" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"</span></pre>" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<span style="color: rgb(106,135,89);">" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"</span>" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@">" withString:@">"]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<span style="color: rgb(0,0,0);">" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<tr><td colspan="1" class="confluenceTd">" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<br />" withString:@""]; mStr = [mStr stringByReplacingOccurrencesOfString:@"<span style="color: rgb(128,128,128);">" withString:@""]; NSArray *arr = nil; if ([obj isEqualToString:regExpString2]) { arr = [mStr componentsSeparatedByString:@"</td><td colspan="1" class="confluenceTd">"]; } else if ([obj isEqualToString:regExpString1]) { arr = [mStr componentsSeparatedByString:@"</td><td class="confluenceTd">"]; } dict[arr.firstObject] = arr[1]; } }]; [dict writeToFile:@"/Users/mac/Desktop/查看源.plist" atomically:YES];
自动生成Plist到桌面,或者你再改成自己的类似这种的需求
/// 字段描述或者字段说明 @property (strong, nonatomic) 字段类型 *字符名称;
晚上10:31更新;利用正则查找,正则替换
- (void)analysisString:(NSString *)inputString { NSString *regExpString2 = @"<tr><td.*?class="confluenceTd">.*?</td><td.*?class="confluenceTd">.*?</td></tr>"; __block NSMutableDictionary *dict = @{}.mutableCopy; [self analysisString:inputString findString:regExpString2 matchBlock:^(NSString *str) { __block NSString *mStr = str.mutableCopy; NSDictionary *replaceDict = @{@"<tr><td.*?class="confluenceTd">":@"", @"</td><td.*?class="confluenceTd">":@"]][[",}; [replaceDict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull value, BOOL * _Nonnull stop) { mStr = [self replace:key withStr:value inString:mStr]; }]; NSArray *arr = [mStr componentsSeparatedByString:@"]][["]; dict[arr.firstObject] = arr[1]; }]; NSLog(@"----%@---", dict); } #pragma mark - private /** 正则查找 @param inputString 操作的string @param regExpString 正则string @param matchBlock 每匹配到一个的时候要操作的代码块 */ - (void)analysisString:(NSString *)inputString findString:(NSString *)regExpString matchBlock:(void (^)(NSString *))matchBlock { __block NSString *htmlStr = inputString.mutableCopy; NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regExpString options:NSRegularExpressionCaseInsensitive error:nil]; NSArray *matches = [regular matchesInString:htmlStr options:0 range:NSMakeRange(0, htmlStr.length)]; for (NSTextCheckingResult *match in matches) { NSRange range = [match range]; NSString *mStr = [htmlStr substringWithRange:range]; mStr = [mStr stringByReplacingOccurrencesOfString:@"</td></tr>" withString:@""]; if (matchBlock) { matchBlock(mStr); } } } /** 正则替换 @param regExpString 正则string @param destString 替换成的string @param orginString 操作的string @return 处理后的string */ - (NSString *)replace:(NSString *)regExpString withStr:(NSString *)destString inString:(NSString *)orginString { // 创建 NSRegularExpression 对象,匹配 正则表达式 NSString *mStr = orginString.mutableCopy; NSRegularExpression *regExp = [[NSRegularExpression alloc] initWithPattern:regExpString options:NSRegularExpressionCaseInsensitive error:nil]; // 替换匹配的字符串为 searchStr mStr = [regExp stringByReplacingMatchesInString:mStr options:NSMatchingReportProgress range:NSMakeRange(0, mStr.length) withTemplate:destString]; return mStr; }