使用正则表达式将数字转换为电话格式
Posted
技术标签:
【中文标题】使用正则表达式将数字转换为电话格式【英文标题】:Convert number to phone format using regular expression 【发布时间】:2013-04-01 09:29:40 【问题描述】:我的问题是我有一些带有数字的文本字段,我必须将此数字转换为某种电话格式,例如 (xxx) xxx-xxxx。我已经用这段代码尝试了一个正则表达式:
wholeText = [wholeText stringByReplacingOccurrencesOfString:@"(\\d1,3)(\\d0,3)(\\d0,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 个数字后应该出现连字符。
【问题讨论】:
这个有用吗? ***.com/questions/123559/… ***.com/questions/6052966/phone-number-formatting 【参考方案1】:使用此实用程序
UITextField 子类,允许以预定义格式输入数字。
http://www.cocoacontrols.com/controls/reformattednumberfield
【讨论】:
【参考方案2】:如果你可以访问惰性操作符,这将做你想做的事(我猜,你没有提供那么多细节。):
/^(\d1,3?)(\d1,3?)(\d1,4)$/
怎么样? Lazy operators.
【讨论】:
【参考方案3】:使用下面的代码
- (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;
试试这个你会成功的
【讨论】:
以上是关于使用正则表达式将数字转换为电话格式的主要内容,如果未能解决你的问题,请参考以下文章
用js的正则表达式检测电话号码,要求必须是1开头,只能是11位数字,该正则表达式怎么写?