Extract single IP address from a string

IanPC

New Member
Joined
Jul 3, 2018
Messages
3
Hello - I have a spreadsheet of URLs, some of which contain IP addresses and some don't. I'm struggling to write an excel formula that will identify the URLs with IP addresses in them, and extract that IP address into another cell. What formula would I paste down column B to achieve this?


URL (column A)Desired output (column B)
http://sample.domain/the/eed/dhgg.html[#Error of some kind]
http://10.22.88.11/eid/tedld/ed.html10.22.88.11
http://242.25.23.7/ede/vkjd/tt.html242.25.23.7
etc

<tbody>
</tbody>

Any help gratefully received. Many thanks.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Yes, in my case the IP addresses will all start with //.
In that case, here is another UDF (user defined function) that you can consider...
Code:
Function IP(s As String) As String
  Dim X As Long, V As Variant
  IP = Split(s & "//", "/")(2)
  If IP Like "*.*.*.*" And Not IP Like "*[!0-9.]*" Then
    For Each V In Split(IP, ".")
      If V > 255 Then
        IP = "No IP"
        Exit Function
      End If
    Next
  Else
    IP = "No IP"
  End If
End Function
 
Upvote 0
In that case, here is another UDF (user defined function) that you can consider...
Pretty likely not possible with the OP's data, but that function returns an invalid IP address for a string like "http://123.0089.34.9/abc" whereas my earlier udf correctly dismisses it, as does this standard worksheet formula - if you don't mind the size of it!
Code:
=IFERROR(IF(AND(
MAX(ABS(52.5-CODE(MID(SUBSTITUTE(MID(A2,FIND("//",A2)+2,FIND("/",A2,9)-FIND("//",A2)-2),".","")&REPT(0,15),ROW(INDIRECT("1:15")),1))))<5,
LEN(MID(A2,FIND("//",A2)+2,FIND("/",A2,9)-FIND("//",A2)-2))-LEN(SUBSTITUTE(MID(A2,FIND("//",A2)+2,FIND("/",A2,9)-FIND("//",A2)-2),".",""))=3,
MAX(LEN(TRIM(MID(SUBSTITUTE(MID(A2,FIND("//",A2)+2,FIND("/",A2,9)-FIND("//",A2)-2),".",REPT(" ",100)),{1,100,200,300},100))))<4,
MAX(MID(SUBSTITUTE(MID(A2,FIND("//",A2)+2,FIND("/",A2,9)-FIND("//",A2)-2),".",REPT(" ",100)),{1,100,200,300},100)+0)<256),
MID(A2,FIND("//",A2)+2,FIND("/",A2,9)-FIND("//",A2)-2),"No IP"),"No IP")
 
Upvote 0
Pretty likely not possible with the OP's data, but that function returns an invalid IP address for a string like "http://123.0089.34.9/abc" whereas my earlier udf correctly dismisses it, as does this standard worksheet formula - if you don't mind the size of it!
While software specific, leading zeros are allowed to make each number 3 digits long. Some software treats numbers like 036 the same as 36, but others would treat that as an Octal number which would make it 30 instead of 36). Given that leading zeros can be evaluated by some systems as being the same as the number without the leading zeros, I would be willing to bet extra leading zeros would be ignored in the same way (not sure how a system that treats 036 as an Octal value would react). Anyway, to address numbers that end up being 4 digits long where the extra digits are zero in the way you have requires only a minor modification to my code...
Code:
Function IP(s As String) As String
  Dim X As Long, V As Variant
  IP = Split(s & "//", "/")(2)
  If IP Like "*.*.*.*" And Not IP Like "*[!0-9.]*" Then
    For Each V In Split(IP, ".")
      If V > 255 Or Len(V) > 3 Then
        IP = "No IP"
        Exit Function
      End If
    Next
  Else
    IP = "No IP"
  End If
End Function
By the way, I would note that an IP address can also be valid with a slashed number at the end which my code does not allow for.[/QUOTE]
 
Last edited:
Upvote 0
@Rick,

Your cod:
Code:
Function IP(s As String) As String
  Dim X As Long, V As Variant
  IP = Split(s & "//", "/")(2)
  If IP Like "*.*.*.*" And Not IP Like "*[!0-9.]*" Then
    For Each V In Split(IP, ".")
      If V > 255 Or Len(V) > 3 Then
        IP = "No IP"
        Exit Function
      End If
    Next
  Else
    IP = "No IP"
  End If
End Function

is not working for http://242.25.423.7/ede/vkjd/tt.html result is No IP
 
Upvote 0
@Rick,

Your cod:

is not working for http:// 242.25.423.7/ede/vkjd/tt.html result is No IP

423 is not an allowed value for a real IP address (the number must be in the range 0 to 255).
 
Last edited:
Upvote 0
I have a similar issue, but my original format is not in HTTP format. The formats are random and I am looking to extract the IP address, if there is one. How can I extract the IP address if the original text is random?

Imagine Encoder 107.8.2.116
Encoder 107.8.3.185
170.8.2.116 Encoder
Imagine 24.174.45.215
 
Upvote 0
try
sourceIP
Imagine Encoder 107.8.2.116107.8.2.116
Encoder 107.8.3.185107.8.3.185
170.8.2.116 Encoder170.8.2.116
Imagine 24.174.45.21524.174.45.215

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    IP = Table.AddColumn(Source, "IP", each Text.Trim(Text.Select([source],{"0".."9","."}),"."))
in
    IP
 
Upvote 0
try
sourceIP
Imagine Encoder 107.8.2.116107.8.2.116
Encoder 107.8.3.185107.8.3.185
170.8.2.116 Encoder170.8.2.116
Imagine 24.174.45.21524.174.45.215

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    IP = Table.AddColumn(Source, "IP", each Text.Trim(Text.Select([source],{"0".."9","."}),"."))
in
    IP
Sandy,

Thanks for the assistance, however, I am not sure exactly how to implement this solution as when I try to build it in VBA, it doesn't seem to work. Is this code just a part of one of the overall larger solutions earlier in this thread or am I missing something here? Sorry if this seems like a stupid question, but I am still very new to most of this.
 
Upvote 0
check the name of CODE (Power Query not a vba!!!)
you've Power Query aka Get&Transform build-in to XL2016
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,681
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top