Jump to content

Find IP address in text using regular expression


jefhal
 Share

Recommended Posts

This took me a few days (dumb), but I finally found a way to find IP addresses in any text string. It does not validate the IP as being a legitimate address, but it will find ip's hidden within other "junk"...

#include <array.au3>
Dim $MyString1
$MyString1 = "junk blah blah blah 192.168.1.1 and other junk 32522344 68.3.1.1 my04 22dd---48s9s"
$answer = StringRegExp($MyString1,'((?:\d+)(?:\.\d+){3})',3)
_ArrayInsert($answer,0,UBound($answer))
_arraydisplay($answer,"$answer")
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

well, it will also detect 1111.2222.3333.4444 as an ip address.

Better do this:

$answer = StringRegExp($MyString1,'((?:\d{1,3}\.){3}\d{1,3})',3)

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

$answer = StringRegExp($MyString1,'((?:\d{1,3}\.){3}\d{1,3})',3)

Thank you!

You're welcome.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Moderators

well, it will also detect 1111.2222.3333.4444 as an ip address.

Better do this:

$answer = StringRegExp($MyString1,'((?:\d{1,3}\.){3}\d{1,3})',3)

Cheers

Kurt

Even that would see something like "300.200.100.000" as an ip address.
Link to comment
Share on other sites

Even that would see something like "300.200.100.000" as an ip address.

well, I never said, that it will only detect a valid IP address :D

You can do that with Regexp as well, however I leave that as an exercise for you guys :wacko:

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

And another thing:

$MyString = "aaaa.bbbb.cccc.dddd.1111.2222.3333.4444.196.128.0.1.300.200.100.000"

The result would be also wrong...

4.196.128.0

and

1.300.200.100

well, i such a string it would be really hard to detect THE one correct IP address anyway. What's more

correct?

196.128.0.1 or 4.196.128.0 or 128.0.1.3 or 8.0.1.3 or ..... So, where is the criteria to decide which

string is THE one valid ip address? Hard to define....

EDIT: I guess one has to define the delimmiting characters for the IP address, like spaces or tabs, or anything else

to get a correct result.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Even that would see something like "300.200.100.000" as an ip address.

While I'd be very interested in seeing the regexp expression that can detect only valid ip addresses, my initial need for this was to parse out ip addresses from the ouput of NSLOOKUP. Since NSLOOKUP only returns valid ip addresses that are in my domain, I really don't need any error checking for my current project. The current code that /dev/null proposed does the job for me.

Any takers on the error checking regexp expression? :D

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

While I'd be very interested in seeing the regexp expression that can detect only valid ip addresses, my initial need for this was to parse out ip addresses from the ouput of NSLOOKUP. Since NSLOOKUP only returns valid ip addresses that are in my domain, I really don't need any error checking for my current project. The current code that /dev/null proposed does the job for me.

Ah, I see. For that, your approach would have done the job as well. :D

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Here's an idea for the error checking version:

#include <array.au3>
Dim $MyString1
$MyString1 = "junk blah blah blah 192.168.1.1 and other junk 32522344 68.3.1.1 my04 22dd---48s9s 100.2000.2.4 300.22.22.1"
$answer1 = StringRegExp($MyString1,'((?:\d+)(?:\.\d+){3})',3)
$answer2 = StringRegExp($MyString1,'((?:[1-2]?[0-9]?[0-9]\.){3}(?:[1-2]?[0-9]?[0-9]))',3)

_ArrayInsert($answer1,0,UBound($answer1))
_arraydisplay($answer1,"$answer")

_ArrayInsert($answer2,0,UBound($answer2))
_arraydisplay($answer2,"$answer")

However, it has a problem with the last ip address in the list. It returns 00.22.22.1 even though it should return nothing... :D:wacko:

Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Of course, it's probably always best to make code as portable as possible. I was so happy to have ~anything~ work after so much effort that I stopped there and posted what I had. Maybe a start for the error checking version would be?:

[1-2]?[0-9]?[0-9].{3}[1-2]?[0-9]?[0-9]
good idea, however, that would also match 290.x.xx, while each part of a valid IP address is restricted to the value 0-255.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

however, that would also match 290.x.xx, while each part of a valid IP address is restricted to the value 0-255.

Well, the following is VERY ugly, but it almost works. I adapted it from something on the web, which permitted answers like 00.42.18.9, but I couldn't get {3} to work, and I could not get rid of the separate octets in the answer array, so I added a loop to delete them:
#include <array.au3>
Dim $MyString1
$MyString1 = "junk blah blah blah 192.168.1.1 and other junk 32522344 68.3.1.1 my04 22dd---48s9s 100.260.2.4 300.22.22.1"
$answer5 = StringRegExp($MyString1,'((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9]))',3)
For $i = UBound($answer5) -1 to 0 step - 1
;MsgBox(0,"mod $i",Mod($i,5) & "   :   " & $answer5[$i])
if Mod($i,5) <> 4 Then
_ArrayDelete($answer5,$i)
EndIf
Next
_ArrayInsert($answer5,0,UBound($answer5))
_arraydisplay($answer5,"$answer5")

Any ideas how to do this without the loop?

Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

  • 4 years later...
  • 6 years later...

Do you have to treat it as an Array.  

I have been trying to use this example to play around with something.

$MyString1 = ClipGet()
$answer = StringRegExp($MyString1,"((?:\d{1,3}\.){3}\d{1,3})",3)

Send($answer)

But $answer ends up being empty. I send ($MyString1) to make sure that it is grabbing what is on the clipboard and it shows the content of the clipboard correctly.

 

Link to comment
Share on other sites

  • Moderators

Br0k30n3,

StringRegExp when used with option 3 (better known as $STR_REGEXPARRAYGLOBALMATCH) unsurprisingly returns an array, not a single string - so you need to use _ArrayDisplay to see what is in the array and then choose which element you will Send (probably the [0] element if only one match is returned).

M23

Edit: Welcome to the AutoIt forums by the way.

 

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...