Re: Detect phone number in NSString
Re: Detect phone number in NSString
- Subject: Re: Detect phone number in NSString
- From: "Dave DeLong" <email@hidden>
- Date: Wed, 16 Apr 2008 09:40:46 -0600
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