正则表达式 STO单号规则

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

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...