正则表达式匹配iOS中的数字范围

时间:2013-09-16 12:37:25

标签: iphone ios regex

我是iOS中的新程序员,在我正在开发的应用程序中,我需要创建一个正则表达式来匹配给出两个数字的数值范围。

我发现这个答案(在Java中)基本上可以满足我的需要。 How to generate a regular expression at runtime to match a numeric range

我已将代码转换为Objective-C,并且在将结果与此网站http://utilitymill.com/utility/Regex_For_Range进行比较时结果不匹配,可能是我在某处犯了错误或代码无法正常运行。

你们中的任何一个人都知道在Objective-C中我能找到类似的东西吗?

这是我转换的代码:

    -(NSString *) BaseRange:(NSString*)num Bool:(BOOL)up BoolLeading:(BOOL)leading1{

    unichar c = [num characterAtIndex:0];
    unichar low;
    unichar high;

    if(up == YES){
        low=c;
    }else{
        if (leading1==YES){
            low = '1';
        }else{
            low='0';
        }
    }


    if(up==YES){
        high ='9';
    }else{
        high = c;
    }

    if(num.length ==1)
        return [self charClass:low Other:high];

    NSString *re = [NSString stringWithFormat:@"%hu (%@)", c,  [self BaseRange:[num substringToIndex:1] Bool:up BoolLeading:NO]];

    if(up)
    {
        low++;
    }else{
        high--;
    }

    if(low <= high){
        NSString * temp = [NSString stringWithFormat:@"%@|%@%@",re, [self charClass:low Other:high], [self nDigits:[num length] -1]];
        re = temp;
    }

    return re;

}

-(NSString *) charClass:(unichar)b Other:(unichar)e{
    if (b==e) {
        return [NSString stringWithFormat:@"%c", b];
    }
    else if(b-e>1)
    {
        return [NSString stringWithFormat:@"[%c-%c]", b, e];
    }
    else
    {
        return [NSString stringWithFormat:@"[%c%c]", b, e];
    }
}

-(NSString *) nDigits:(int)n{
    return [self nDigits:n otherDigit:n];
}

-(NSString *) nDigits:(int)n otherDigit:(int)m{

    if (n==m) {
        return @"[0-9]";
    }else if (n==1){
        return [NSString stringWithFormat:@"[0-9]{%d}", n];
    }else{
        return [NSString stringWithFormat:@"[0-9]{%d,%d}", n,m];
    }
}


-(NSString *) eqLengths:(NSString *)from OtherString:(NSString*)to{
    unichar fc = [from characterAtIndex:0];
    unichar tc = [to characterAtIndex:0];

    if(from.length == 1 && to.length == 1){
        return [self charClass:fc Other:tc];
    }

    if(fc == tc){
        return [NSString stringWithFormat:@"%hu(%@)",fc,[self rangeRegex:[from substringToIndex:1] otherValue:[to substringToIndex:1]]];
    }

    NSString *re = [NSString stringWithFormat:@"%hu(%@)|%hu(%@)",fc,[self BaseRange:[from substringFromIndex:1] Bool:YES BoolLeading:NO],tc,[self BaseRange:[to substringFromIndex:1] Bool:NO BoolLeading:NO]];

    if (++fc <=--tc) {
        re = [NSString stringWithFormat:@"%@|%@%@", re,[self charClass:fc Other:tc],[self nDigits:[from length]-1]];
    }
    return re;
}


-(NSString *) nonEqLengths:(NSString *)from OtherString:(NSString *)to{
    NSString *re = [NSString stringWithFormat:@"%@|%@",[self BaseRange:from Bool:YES BoolLeading:NO],[self BaseRange:to Bool:NO BoolLeading:YES]];

    if ([to length] - [from length] > 1)
        re = [NSString stringWithFormat:@"%@|[1-9]%@",re,[self nDigits:[from length] otherDigit:[to length]-2]];
        //re += "|[1-9]" + nDigits([from length], [to length] - 2);
    return re;
}

-(NSString *) rangeRegex:(NSString *)n otherValue:(NSString *)m{
    if(n.length == m.length)
    {
        return [self eqLengths:n OtherString:m];
    }else{
        return [self nonEqLengths:n OtherString:m];
    }
}

结果: 范围100 - 1000

我的代码结果:49([19])| [29] [0-9] | 49([01])

网站的预期结果:([1-9] [0-9] {2} | 1000)

范围0 - 5000

我的代码的结果:[09] | 53([05])| [14] [0-9] | [1-9] [0-9] {1}

网站的预期结果:^([0-9] {1,3} | [1-4] [0-9] {3} | 5000)$

0 个答案:

没有答案