菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
310
0

简单的正则表达式拼接类

原创
05/13 14:22
阅读数 72013
    TigerRegular *reg = [TigerRegular new];
    [reg addOptionalTextItems:@[@"http", @"ftp", @"wc"] minTimes:1 maxTimes:1];
    [reg addStaticText:@"s" minTimes:0 maxTimes:1];
    [reg addStaticText:@"://" minTimes:1 maxTimes:1];
    [reg addCharacterInText:@"0-9A-Za-z" minLength:1 maxLength:INT_MAX];
    TigerRegular *domainReg = [TigerRegular new];
    [domainReg addStaticText:@"." minTimes:1 maxTimes:1];
    [domainReg addCharacterInText:@"0-9A-Za-z" minLength:1 maxLength:INT_MAX];
    [reg addSubRegular:domainReg.regular minTimes:0 maxTimes:3];
    TigerRegular *portReg = [TigerRegular new];
    [portReg addStaticText:@":" minTimes:1 maxTimes:1];
    [portReg addCharacterInText:@"0-9" minLength:1 maxLength:4];
    [reg addSubRegular:portReg.regular minTimes:0 maxTimes:INT_MAX];
    TigerRegular *pathReg = [TigerRegular new];
    [pathReg addStaticText:@"/" minTimes:1 maxTimes:1];
    [pathReg addCharacterInText:@"0-9A-Za-z" minLength:1 maxLength:INT_MAX];
    [reg addSubRegular:pathReg.regular minTimes:0 maxTimes:INT_MAX];
    [reg addSubRegular:domainReg.regular minTimes:0 maxTimes:1];
    NSLog(@"%@", reg.regular);
@interface TigerRegular : NSObject

@property (nonatomic, copy, readonly) NSString *regular;

- (void)addStaticText:(NSString*)text minTimes:(int)minTimes maxTimes:(int)maxTimes;
- (void)addCharacterInText:(NSString*)text minLength:(int)minLength maxLength:(int)maxLength;
- (void)addCharacterNotInText:(NSString*)text minLength:(int)minLength maxLength:(int)maxLength;
- (void)addOptionalTextItems:(NSArray<NSString*>*)textItems minTimes:(int)minTimes maxTimes:(int)maxTimes;
- (void)addSubRegular:(NSString*)regular minTimes:(int)minTimes maxTimes:(int)maxTimes;

@end
#import "TigerRegular.h"

typedef NSString * TigerRegularRange;
typedef NSString * TigerRegularOptional;
typedef NSString * TigerRegularPart;
typedef NSString * TigerRegularItem;
typedef NSString * TigerRegularRepeat;
typedef NSString * TigerRegularConfiguration;

@interface TigerRegular ()

@property (nonatomic, strong) NSMutableString *tigerRegular;

@end

@implementation TigerRegular

- (instancetype)init {
    self = [super init];
    if (self) {
        self.tigerRegular = [NSMutableString string];
    }
    return self;
}

- (void)addStaticText:(NSString*)text minTimes:(int)minTimes maxTimes:(int)maxTimes {
    TigerRegularPart part = [self partWithItem:[self encodeItem:text]];
    TigerRegularConfiguration configuration = [self configurationWithPart:part repeat:[self repeat:minTimes maxTimes:maxTimes]];
    [self.tigerRegular appendString:configuration];
    
}
- (void)addCharacterInText:(NSString*)text minLength:(int)minLength maxLength:(int)maxLength {
    TigerRegularRange range = [self range:[self encodeItem:text]];
    TigerRegularPart part = [self belong:range];
    TigerRegularConfiguration configuration = [self configurationWithPart:part repeat:[self repeat:minLength maxTimes:maxLength]];
    [self.tigerRegular appendString:configuration];
}
- (void)addCharacterNotInText:(NSString*)text minLength:(int)minLength maxLength:(int)maxLength {
    TigerRegularRange range = [self range:[self encodeItem:text]];
    TigerRegularPart part = [self notBelong:range];
    TigerRegularConfiguration configuration = [self configurationWithPart:part repeat:[self repeat:minLength maxTimes:maxLength]];
    [self.tigerRegular appendString:configuration];
}
- (void)addOptionalTextItems:(NSArray<NSString*>*)textItems minTimes:(int)minTimes maxTimes:(int)maxTimes {
    NSMutableArray <TigerRegularItem> *inputItems = [NSMutableArray array];
    for (TigerRegularItem item in textItems) {
        [inputItems addObject:[self encodeItem:item]];
    }
    TigerRegularOptional optional = [self optional:inputItems.copy];
    TigerRegularConfiguration configuration = [self configurationWithOptional:optional repeat:[self repeat:minTimes maxTimes:maxTimes]];
    [self.tigerRegular appendString:configuration];
    
}
- (void)addSubRegular:(NSString*)regular minTimes:(int)minTimes maxTimes:(int)maxTimes {
    TigerRegularConfiguration configuration = [self configurationWithSubConfiguration:regular repeat:[self repeat:minTimes maxTimes:maxTimes]];
    [self.tigerRegular appendString:configuration];
}

- (TigerRegularRepeat)repeat:(int)minTimes maxTimes:(int)maxTimes {
    if (minTimes == 0 && maxTimes == 1) {
        return self.lessOrEqualOnce;
    }
    if (minTimes == 0 && maxTimes == INT_MAX) {
        return self.anyTimes;
    }
    if (minTimes == 1 && maxTimes == INT_MAX) {
        return self.moreThenOnce;
    }
    if (minTimes == maxTimes) {
        return [self matchTimes:minTimes];
    }
    if (maxTimes == INT_MAX) {
        return [self matchMoreThenTimes:minTimes];
    }
    return [self matchTimes:minTimes max:maxTimes];
}

- (NSString *)regular {
    return self.tigerRegular.copy;
}

- (TigerRegularConfiguration)configurationWithOptional:(TigerRegularOptional)optional repeat:(TigerRegularRepeat)repeat {
    return [NSString stringWithFormat:@"(%@)%@", optional, repeat];
}

- (TigerRegularConfiguration)configurationWithPart:(TigerRegularPart)part repeat:(TigerRegularRepeat)repeat {
    return [NSString stringWithFormat:@"(%@)%@", part, repeat];
}

- (TigerRegularConfiguration)configurationWithSubConfiguration:(TigerRegularConfiguration)subConfiguration repeat:(TigerRegularRepeat)repeat {
    return [NSString stringWithFormat:@"(%@)%@", subConfiguration, repeat];
}

- (TigerRegularPart)partWithItem:(TigerRegularItem)item {
    return item;
}

- (TigerRegularRange)range:(TigerRegularRange)range appendRange:(TigerRegularRange)otherRange {
    return [NSString stringWithFormat:@"%@%@", range, otherRange];
}

// 属于某一个字符
- (TigerRegularPart)belong:(TigerRegularRange)range {
    return [NSString stringWithFormat:@"[%@]", range];
}

// 不属于某一个字符
- (TigerRegularPart)notBelong:(TigerRegularRange)range {
    return [NSString stringWithFormat:@"[^%@]", range];
}

// 可选值
- (TigerRegularOptional)optional:(NSArray<TigerRegularItem>*)optional {
    return [optional componentsJoinedByString:@"|"];
}

// 可选字符
- (TigerRegularRange)range:(TigerRegularRange)range {
    return range;
}

// 字符区间
- (TigerRegularRange)range:(TigerRegularItem)start end:(TigerRegularItem)end {
    return [NSString stringWithFormat:@"%@-%@", start, end];
}

// 数字
- (TigerRegularItem)number {
    return @"\\d";
}

// 字符
- (TigerRegularItem)character {
    return @"\\w";
}

// 空格
- (TigerRegularItem)blankSpace {
    return @"\\s";
}


// 数字
- (TigerRegularItem)notNumber {
    return @"\\D";
}

// 字符
- (TigerRegularItem)notCharacter {
    return @"\\W";
}

// 空格
- (TigerRegularItem)notBlankSpace {
    return @"\\S";
}

// 不是换行
- (TigerRegularItem)notNewLine {
    return @".";;
}

// 最多出现一次
- (TigerRegularRepeat)lessOrEqualOnce {
    return @"?";
}

// 任意次
- (TigerRegularRepeat)anyTimes {
    return @"*";
}

// 超过1次
- (TigerRegularRepeat)moreThenOnce {
    return @"+";
}

// 字符串开头
- (TigerRegularConfiguration)isPrefix {
    return @"^";
}

// 字符串结尾
- (TigerRegularConfiguration)isSuffix {
    return @"$";
}

- (NSArray<TigerRegularItem>*)keywordItems {
    static NSArray<TigerRegularItem> *items = nil;
    if (!items) {
        items = @[self.notNewLine, self.lessOrEqualOnce, self.anyTimes, self.moreThenOnce, self.isPrefix, self.isSuffix];
    }
    return items;
}

- (TigerRegularItem)encodeItem:(TigerRegularItem)item {
    TigerRegularItem newItem = item;
    for (TigerRegularItem object in self.keywordItems) {
        newItem = [newItem stringByReplacingOccurrencesOfString:object withString:[NSString stringWithFormat:@"\\%@", object]];
    }
    return newItem;
}

// 出现N次
- (TigerRegularRepeat)matchTimes:(int)times {
    return [NSString stringWithFormat:@"{%d}", times];
}

// 出现N次以上
- (TigerRegularRepeat)matchMoreThenTimes:(int)times {
    return [NSString stringWithFormat:@"{%d,}", times];
}

// 出现M-N次之间
- (TigerRegularRepeat)matchTimes:(int)min max:(int)max {
    return [NSString stringWithFormat:@"{%d,%d}", min, max];
}

@end

 

发表评论

0/200
310 点赞
0 评论
收藏
为你推荐 换一批