extracting ip address from url

Hi all,

Is there a way to extract an ip-address from a URL (e.g. >
I found that ip addresses vary in length (am i correct?) so this makes it harder to extract them.

Basically i need this as a part of validating a url (Not necessarily reachable via interwebs or reachable at all).
So the mac won’t do things (try to access) those url’s if they aren’t even a real URL.



This should work.

set b to getIP from ""
to getIP from anUrl
	local a, b, ipAddr
	set a to offset of "//" in anUrl
	set b to offset of "/" in (text (a + 2) thru -1 of anUrl)
	set ipAddr to text (a + 2) thru (a + b) of anUrl
	return ipAddr
end getIP

Or with an regular expression (TS isn’t asking for hostname)


on getIPFromString(str)
	return do shell script "echo " & quoted form of str & " | grep -Eo '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'"
end getIPFromString

I actually fixed it with this:

on stripIP_()
		set IpAddress to text item 3 of location
	on error
		display dialog noslash
	end try
	log "Ip: \"" & IpAddress & "\" extracted from \"" & location & "\""
end stripIP_

and the delimiters subroutine:

on delimiters_(a)
	set a to a as text
	set AppleScript's text item delimiters to a
end delimiters_

  • Dj Bazzie Wazzie, i know i should not use the try syntax, but can’t help it!