1

I have a very big NSString, which holds around 1500 characters in it. In this string I need to extract a phone number, which may change frequently, as it is a dynamic data. The phone number will be in the format of 251-221-2000, how can I extract this?

Zeta
  • 100,191
  • 13
  • 186
  • 227
Pradeep Kumar
  • 401
  • 5
  • 18
  • Check [NSScanner](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html%23//apple_ref/occ/cl/NSScanner) – iDev Jan 07 '13 at 05:29

3 Answers3

5

Check out this previous question on regular expressions and NSString.

Search through NSString using Regular Expression

In your case an appropriate regular expression would be @"\\d{3}-\\d{3}-\\d{4}".

Community
  • 1
  • 1
s.bandara
  • 5,576
  • 1
  • 20
  • 36
0

This sounds like a perfect candidate for a regular expression. You can use the NSRegularExpression class to achieve this. You can test your regular expression at http://www.regextester.com

borrrden
  • 32,952
  • 8
  • 73
  • 109
0
NSString *yourString = @"Your 1500 characters string ";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression         
regularExpressionWithPattern:@"\d{3}-\d{3}-\d{4}"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
// your code to handle matches here
}];  

Let me know it is working or not.

MCKapur
  • 9,087
  • 9
  • 57
  • 100
NiravPatel
  • 3,270
  • 2
  • 19
  • 31