使用正则表达式将数字转换为电话格式

时间:2013-04-01 09:29:40

标签: iphone ios regex ios5 formatting

我的问题是我有一些带号码的文字字段,我必须将此号码转换为某种电话格式,例如(xxx)xxx-xxxx 。我已尝试使用此代码的正则表达式:

wholeText = [wholeText stringByReplacingOccurrencesOfString:@"(\\d{1,3})(\\d{0,3})(\\d{0,4})"
                                                     withString:@"($1) $2-$3"
                                                        options:NSRegularExpressionSearch
                                                          range:NSMakeRange(0, wholeText.length)];
NSLog(@"wholeText = %@", wholeText);

如果我逐渐在文本字段中输入文字,NSLog会输出:

wholeText = (1) -
wholeText = (12) -
wholeText = (123) -
wholeText = (123) 4-
wholeText = (123) 45-
wholeText = (123) 456-
wholeText = (123) 456-7

所以我的问题是,如果之前没有数字,我不需要括号和连字符,即在输入第4个数字后应出现结束括号,并且在输入第7个数字后应出现连字符。< / p>

3 个答案:

答案 0 :(得分:0)

使用下面的代码

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    int length = [self getLength:textField.text];
    //NSLog(@"Length  =  %d ",length);

    if(length == 10)
    {
        if(range.length == 0)
            return NO;
    }

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSCharacterSet *charactersToRemove = [[ NSCharacterSet alphanumericCharacterSet ] invertedSet ];

    newString = [[newString componentsSeparatedByCharactersInSet:charactersToRemove]componentsJoinedByString:@""];

    NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression 
                                                                           options:NSRegularExpressionCaseInsensitive 
                                                                             error:nil];
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                        options:0
                                                          range:NSMakeRange(0, [newString length])];
    NSLog(@"newString::%@",newString);
    if (numberOfMatches == 0)
        return NO; 


    if(length == 3)
    {
        NSString *num = [self formatNumber:textField.text];
        textField.text = [NSString stringWithFormat:@"(%@)",num];
        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]];
    }
    else if(length == 6)
    {
        NSString *num = [self formatNumber:textField.text];
        //NSLog(@"%@",[num  substringToIndex:3]);
        //NSLog(@"%@",[num substringFromIndex:3]);
        textField.text = [NSString stringWithFormat:@"(%@) %@-",[num  substringToIndex:3],[num substringFromIndex:3]];
        if(range.length > 0)
            textField.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]];
    }
    return YES;
}

#pragma mark - Mobile Validation

-(NSString*)formatNumber:(NSString*)mobileNumber
{

    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
    mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

    NSLog(@"%@", mobileNumber);

    int length = [mobileNumber length];
    if(length > 10)
    {
        mobileNumber = [mobileNumber substringFromIndex: length-10];
        NSLog(@"%@", mobileNumber);

    }


    return mobileNumber;
}


-(int)getLength:(NSString*)mobileNumber
{

     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
     mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];

     int length = [mobileNumber length];

     return length;
}

试试这个你会成功的

答案 1 :(得分:0)

使用此实用程序

enter image description here

UITextField子类,允许以预定义格式输入数字。

http://www.cocoacontrols.com/controls/reformattednumberfield

答案 2 :(得分:0)

如果您可以访问懒惰操作符,这将完成您想要的操作(我猜,您没有提供太多详细信息。):

/^(\d{1,3}?)(\d{1,3}?)(\d{1,4})$/

如何? Lazy operators.