0

I'm a new programmer in iOS and in the app I'm developing I have the need to create a regular expression to match a numeric range giving two numbers.

I found this answer(in Java) that basically does what I need. How to generate a regular expression at runtime to match a numeric range

I have converted the code to Objective-C and when comparing the results with this site http://utilitymill.com/utility/Regex_For_Range the results don't match, probably I've made a mistake somewhere or the code does not work properly.

Any of you guys have know where I can find something similar but in Objective-C?

Here's my converted code:

    -(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];
    }
}

Results: Range 100 - 1000

Result from my code: 49 ([19])|[29][0-9]|49 ([01])

Expected result from the website: ([1-9][0-9]{2}|1000)

Range 0 - 5000

Result from my code: [09]|53 ([05])|[14][0-9]|[1-9][0-9]{1}

Expected result from the website: ^([0-9]{1,3}|[1-4][0-9]{3}|5000)$

4

0 回答 0