Is there a way to extract an ip-address from a URL (e.g. ftp://127.0.0.1/path/to/file/ > 127.0.0.1)?
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.
set b to getIP from "https://127.0.0.1/path/to/file/"
-->"127.0.0.1"
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)
getIPFromString("https://127.0.0.1/path/to/file/")
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
on stripIP_()
delimiters_("/")
try
set IpAddress to text item 3 of location
on error
display dialog noslash
end try
log "Ip: \"" & IpAddress & "\" extracted from \"" & location & "\""
delimiters_("")
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!