On Mar 3, 2012, at 4:15 PM, R wrote:

> I'm using NSRegularExpression to build an array of all NSRanges in the
> text field that start with "http".  I then use NSScanner to scan until
> it finds whitespace or end of line characters for each
> range.location.  I then place this new subString into a NSURL to
> validate that it does not return nil.

Um, why not use NSRegularExpression to find the whole thing? /http://\S+/
Actually if you search around you can probably find some better regexps for 
matching URLs. Requiring whitespace at the end is sort of fragile; people often 
put URLs in sentences with a comma or period afterwards.

Also, URLs don’t have to start with “http”. There are FTP URLs, mailto: URLs 
and more. And it’s pretty common to omit the “http:” part and write the URL 
like www.apple.com, which most parsers will recognize.

> I'm finding that NSURL pretty much likes everything.....
> http://www.cnn.thePiratesWillWinTheWorldSeries is happy.

Unfortunately that is a syntactically valid URL with a syntactically valid 
domain name. There just doesn’t happen to be a top-level-domain [yet] named 
“thePiratesWillWinTheWorldSeries”, but for all you know, next week somebody 
could buy that TLD and register the domain.

It’s also possible that one could be in a network environment in which DNS is 
set to have a default domain like “corp.example.com” (pretty common in an 
intranet), in which case that hostname could resolve to 
“www.cnn.thePiratesWillWinTheWorldSeries.corp.example.com”. Again, unlikely but 
possible.

Realistically, whatever message you found that URL in is messed up. Don’t even 
try to untangle that.

> Is there a good way to validate a NSURL for the internet, short of
> pinging?

Look up the hostname. No ping required.

> My next step is to find the end of the substring, count back three
> characters and check for com,edu or org... seems clunky and inaccurate
> for future internet standards.

It’s a lot more than those three! There are already something like 200 other 
top-level domains (including one for every country on Earth), and as I said 
above, ICANN is about to start selling off new ones to people willing to pay a 
lot of money.

Honestly, use Data Detectors.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to