My problem is that I have some text field with number and I must to convert this number to some phone format like this one (xxx) xxx-xxxx. I have tried an regular expression with this code:
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);
If I gradually enter a text in text field, NSLog
output this:
wholeText = (1) -
wholeText = (12) -
wholeText = (123) -
wholeText = (123) 4-
wholeText = (123) 45-
wholeText = (123) 456-
wholeText = (123) 456-7
So my problem that I do not need brackets and hyphens if there is no number before it, i.e. closing bracket should appear after I enter 4th number and hyphen should appear after I enter 7th number.