• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Detect phone number in NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Detect phone number in NSString


  • Subject: Re: Detect phone number in NSString
  • From: Mike Wright <email@hidden>
  • Date: Wed, 16 Apr 2008 13:04:55 -0400

On 16 Apr 2008 09:40:46 -0600, Dave DeLong" <email@hidden> wrote:

(Guess I have to keep top posting.)

If you're only concerned with US phone numbers, about a half-dozen canned patterns should suffice. However, if you need to deal with phone numbers in places like Australia, Japan, and France, the number quickly increases.

What I do in my software (iData 3) is to provide a preference item where users can specify the patterns they, themselves, use. You can see the Help page for this at <http://www.idata3.com/idata3help/iD3PrefsDialing.html#extension >. It shows a set of default patterns in the Telephone Number Templates field. There's a brief explanation farther down the page.

Using iData's template notation, non-US phone numbers can contain such patterns as:

nnnn-nnn
nnnnn-nn
nnn.nnn.nnnn
nn nn nnn.nnn-nnnn

Rather than test against lots of irrelevant patterns--and still risk missing some--it's better to let the user define the set of expected patterns. (I'm serious. I've had all of these, and more, thrown at me before I finally broke down and started making the users define their own.)

I don't use regular expressions--the basic code goes back to System 7 or 8, just updated for Cocoa. What I do is to scan the text (from the end of the current selection) for the first digit:

NSString * myString; // derived from NSTextView content
NSRange numeralRange = [myString rangeOfCharacterFromSet: [NSCharacterSet decimalDigitCharacterSet]];


Then I test the following text to see if it matches any of the patterns. If it does, it gets dialed. If not, I search on from there-- and wrap to the beginning of the text if appropriate.

Having found a numeric character, the testing algorithm is approximately:

1. Find the potential end of the phone number, the earliest of:
a. End of the string
b. End of line (\n)
c. Three non-numerics in a row. (This is an assumption on my part.
Four or five might be safer.)
2. Copy the resulting range of myString to an NSMutableString.
3. Change all numerals in the mutable string to 'n'.
4. Compare the resulting copy to each string in template list. If any is equal, return the potential number string as the real thing.


Unless the text contains *lots* of other numbers, it's quite fast. (And, as you'll see from the Help page, the user can always force the dialing of any particular number, regardless of format, by highlighting it, or by enclosing it in user-defined brackets.)

Mike Wright
http://www.idata3.com/


The thing that first comes to mind would be to use regular expressions.
CocoaDev has a good page listing where you could find some Regex
frameworks:
http://www.cocoadev.com/index.pl?RegularExpressions


Then you'd run your string through a matcher while looking for the phone
number pattern. You could use a pattern similar to:


(\(\d{3}\)\s?)?\d{3}[-\s\.]\d{4} (UNTESTED)
This will look for a phone number that has an area code in parenthesis (but
that is optional), followed by three digits, a hyphen, space, or period, and
then four digits.


If your regex returns a successful match, then you "know" a phone number is
in the string.


Googling should lead you to some much "cleaner" regexes for phone numbers,
since those are a relatively common thing to search for.


HTH,

Dave DeLong

On Wed, Apr 16, 2008 at 2:47 AM, Brad Peterson <email@hidden> wrote:

Hi all,

Like many cell phones are doing these days, I want to
be able to detect phone numbers in NSStrings and
highlight them in some way for the user.

So, if I had some text, say : "Hi Tom, Please call
Cheryl at 444-555-6767 and she can get you that
info..." I want to be able to "know" that 444-555-6767
is a phone number and should be marked as such.

I've searched the archives, but didn't find anything
on this list. I thought about using NSString's isLike:
method, but that doesn't necessarily help me locate
the position of the phone number, IIRC; merely to know
that there might be one.

Any thoughts would be greatly appreciated.

Thank you!

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Detect phone number in NSString
  • Next by Date: Re: binary search trees & binning
  • Previous by thread: Re: Detect phone number in NSString
  • Next by thread: NSValue value:withObjCType:
  • Index(es):
    • Date
    • Thread