簡體   English   中英

使用正則表達式將號碼轉換為電話格式

[英]Convert number to phone format using regular expression

我的問題是我有一些帶有數字的文本字段,我必須將此數字轉換為某種電話格式,例如(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個數字后應出現連字符。

使用以下代碼

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

試試這個,你會成功的

使用這個工具

在此處輸入圖片說明

UITextField子類,允許以預定義格式輸入數字。

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

如果您可以使用惰性運算符,則可以執行您想要的操作(我想,您沒有提供太多詳細信息。):

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

怎么樣? 惰性運算符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM