STO APP项目 单号规则
其实单号规则很容易写的 开始我也不太了解正则表达式 我只是看了一下安卓代码 然后写了个OC版本的 一次成功
全代码如下 就一个CodeMatches类
#import <Foundation/Foundation.h> //单号规则 @interface CodeMatches : NSObject + (NSMutableArray *)startsWithFor12; //12位运单号 规则 + (NSMutableArray *)startsWithFor13; //13位运单号 规则 + (BOOL)matches:(NSString *)billcode;//传入运单号 进行规则对比 是否符合单号规则 + (BOOL)matchesPackeg:(NSString *)pacCode; //传入集包单号 进行规则对比 是否符合单号规则 + (BOOL )startWith:(NSString *)start billcode:(NSString *)billcode; //进行对比 @end
#import "CodeMatches.h" @implementation CodeMatches + (BOOL)matches:(NSString *)billcode{ if(billcode == nil){ return NO; } if (![[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"\\d+"] evaluateWithObject:billcode] ) { return NO; } BOOL matches = NO; switch (billcode.length){ case 12:{ for(int i=0; i<[[CodeMatches startsWithFor12] count]; i++){ if([CodeMatches startWith:[CodeMatches startsWithFor12][i] billcode:billcode]){ matches = YES; break; } } break; } case 13:{ for(int i=0; i<[[CodeMatches startsWithFor13] count]; i++){ if([CodeMatches startWith:[CodeMatches startsWithFor13][i] billcode:billcode]){ matches = YES; break; } } break; } } return matches; } + (BOOL)matchesPackeg:(NSString *)pacCode{ if(pacCode == nil){ return NO; } if (![[NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"\\d+"] evaluateWithObject:pacCode] ) { return NO; } BOOL matches = NO; switch (pacCode.length){ case 12:{ if ([[pacCode substringToIndex:3] isEqualToString:@"900"]) { matches = YES; } break; } } return matches; } + (NSMutableArray *)startsWithFor12{ static NSMutableArray *array = nil; if (array == nil) { array = [NSMutableArray arrayWithObjects:@"268",@"368",@"468",@"568",@"768",@"868",@"968",@"40",@"588",@"688",@"888",@"11",@"22",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",nil]; } return array; } + (NSMutableArray *)startsWithFor13{ static NSMutableArray *array = nil; if (array == nil) { array = [NSMutableArray arrayWithObjects:@"33",@"55",@"660",@"661",@"662",@"663",@"664",@"665",@"666",@"667",@"669",@"77",@"880",@"881",@"882",@"883",@"884",@"885",@"886",@"887",@"889",@"99",nil]; } return array; } - (NSString *)matches{ return @"\\d+"; } + (BOOL )startWith:(NSString *)start billcode:(NSString *)billcode{ if (start.length == 2) { if([[billcode substringToIndex:2]isEqualToString:start]){ return YES; }else{ return NO; } } if (start.length == 3) { if([[billcode substringToIndex:3]isEqualToString:start]){ return YES; }else{ return NO; } } return YES; } @end原文链接:https://www.f2er.com/regex/358639.html