Search code examples
autoit

Autoit script to extract all ip address from string


I am new in autoit, I want to extract all ip address from this string in array format

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <array.au3>

$str = "ghg shjsja 192.168.1.2 hbkjNKKSJKKN HKJCBKJLKKL 12.15.14.45 
        KJBKJABCKBNDKQ djfsjdkfhnwk kjwenfkjdsnf knfflksnf KHBKJABCKJQDH 
        1.1.1.1 2.2.2.2"

$copy = StringRegExp($str,"((\d{1,3}\.){3}\d{1,3})",3)
MsgBox(0,"",$copy[0])`

i am getting all ip when i skip one step, means i will get second ip when i use $copy[2]. i want to extract all ip without skipping the step.


Solution

  • $str = "ghg shjsja 192.168.1.2 hbkjNKKSJKKN HKJCBKJLKKL 12.15.14.45 " & _
           "KJBKJABCKBNDKQ djfsjdkfhnwk kjwenfkjdsnf knfflksnf KHBKJABCKJQDH " & _
           "1.1.1.1 2.2.2.2"
    
    $copy = StringRegExp($str, "((?:\d{1,3}\.){3}\d{1,3})", 3)
    
    For $i = 0 To UBound($copy) -1
        ConsoleWrite($copy[$i] & @CRLF)
        MsgBox(0, "", $copy[$i])
    Next
    

    Will output

    192.168.1.2
    12.15.14.45
    1.1.1.1
    2.2.2.2
    

    instead of

    192.168.1.2
    1.
    12.15.14.45
    14.
    1.1.1.1
    1.
    2.2.2.2
    2.
    

    The ?: at the start of the inner capture group makes it not capture and be just a group. The outer group captures the inner group along with other characters within the outer capture group as a single [step|capture].