josheee12 Posted May 12, 2009 Share Posted May 12, 2009 i wrote code that reads ip adresses from some ftp server software i have, in it's logging window. i need to remove duplicate addresses from the string that holds the list, and also removes addresses that start with 192.168.*.* and 127.0.0.1. it is not in an array. it is in a list seperated by commas. please help me. Link to comment Share on other sites More sharing options...
TurionAltec Posted May 12, 2009 Share Posted May 12, 2009 I assume some sort of ridiculous RegEx could be used, but I'd use StringSplit to separate the comma separated values into an array, then search through the array for invalid addresses For $i = 1 to $array[0] If ($array[$i] == "127.0.0.1") OR stringleft($array[$i],7)=="192.168" Then _ArrayDelete($array[$i]) $i -=1 $array[0]-=1 Endif Next Link to comment Share on other sites More sharing options...
weaponx Posted May 12, 2009 Share Posted May 12, 2009 Here is one way: #include <array.au3> $string = "15.23.55.67,30.21.34.245,192.168.1.2,45.66.94.100,192.168.1.3,30.21.34.245,127.0.0.1" $aArray = StringSplit($string,",",2) For $V = Ubound($aArray)-1 to 0 Step -1 ;Strip 192.168.*.* and 127.0.0.1 If StringLeft($aArray[$V],7) = "192.168" OR $aArray[$V] = "127.0.0.1" Then _ArrayDelete($aArray, $V) Else ;Remove current element if duplicate is found $Index = _ArraySearch($aArray,$aArray[$V],0,$V) If NOT @ERROR AND ($V <> $Index) Then _ArrayDelete($aArray, $Index) EndIf EndIf Next For $X = 0 to Ubound($aArray)-1 ConsoleWrite("[" & $X & "]: " & $aArray[$X] & @CRLF) Next Output: [0]: 15.23.55.67 [1]: 45.66.94.100 [2]: 30.21.34.245 Link to comment Share on other sites More sharing options...
TurionAltec Posted May 12, 2009 Share Posted May 12, 2009 (edited) I fixed these two lines:$aArray = StringSplit($string,",",0)For $X = 1 to Ubound($aArray)-1#include <array.au3> $string = "15.23.55.67,30.21.34.245,192.168.1.2,45.66.94.100,192.168.1.3,30.21.34.245,127.0.0.1" $aArray = StringSplit($string,",",0) For $V = Ubound($aArray)-1 to 0 Step -1 ;Strip 192.168.*.* and 127.0.0.1 If StringLeft($aArray[$V],7) = "192.168" OR $aArray[$V] = "127.0.0.1" Then _ArrayDelete($aArray, $V) Else ;Remove current element if duplicate is found $Index = _ArraySearch($aArray,$aArray[$V],0,$V) If NOT @ERROR AND ($V <> $Index) Then _ArrayDelete($aArray, $Index) EndIf EndIf Next For $X = 1 to Ubound($aArray)-1 ConsoleWrite("[" & $X & "]: " & $aArray[$X] & @CRLF) Nextoutput:[1]: 15.23.55.67 [2]: 45.66.94.100 [3]: 30.21.34.245 Edited May 12, 2009 by TurionAltec Link to comment Share on other sites More sharing options...
weaponx Posted May 12, 2009 Share Posted May 12, 2009 Thats not fixing it. The element count is in [0] and there is no reason to compare that element when searching the array. Also that number will be wrong once the array has been cleaned anyways. Link to comment Share on other sites More sharing options...
weaponx Posted May 12, 2009 Share Posted May 12, 2009 This regular expression is supposed to remove duplicate items from a comma separated string but it doesn't work for me.;$string = "15.23.55.67,30.21.34.245,192.168.1.2,45.66.94.100,192.168.1.3,30.21.34.245,127.0.0.1" $string = "test,animal,cat,mouse,dog,animal,something" $result = StringRegExpReplace($string, "(?<=,|^)([^,]*)(,\1)+(?=,|$)", "$0") If @ERROR Then MsgBox(0,"",@ERROR) ConsoleWrite(@EXTENDED & ": " & $result & @CRLF)Source:http://www.regular-expressions.info/duplicatelines.html Link to comment Share on other sites More sharing options...
TurionAltec Posted May 12, 2009 Share Posted May 12, 2009 Thats not fixing it. The element count is in [0] and there is no reason to compare that element when searching the array. Also that number will be wrong once the array has been cleaned anyways. I guess 2 was added as a parameter in a newer version. The one I was running didn't have it and output [0]: 1 [1]: 15.23.55.67,30.21.34.245,192.168.1.2,45.66.94.100,192.168.1.3,30.21.34.245,127.0.0.1 Link to comment Share on other sites More sharing options...
oMBRa Posted May 12, 2009 Share Posted May 12, 2009 (edited) try this: $string = "15.23.55.67,30.21.34.245,192.168.1.2,45.66.94.100,192.168.1.3,30.21.34.245,127.0.0.1,8.8.8.8,2.2 .2.2,3.3.3.3,2.2.2.2" $a = StringRegExpReplace($string, '192\.168(\.\d{1,3}){2},?|127\.0\.0\.1,?', '') $a = StringRegExpReplace($string, '((\d{1,3}\.?){4}),?(.*?)(\1)', '\3\4') MsgBox(0, '', $a) Edited May 12, 2009 by oMBRa Link to comment Share on other sites More sharing options...
josheee12 Posted May 12, 2009 Author Share Posted May 12, 2009 (edited) here´s an updatei got it to work, partiallynow i need to filter out everything that does not take the form of *.*.*.*the items are seperated by pipesnow i have FileZilla Server version 0.9.31 betaCopyright 2001-2009 by Tim Kosse (tim.kosse@filezilla-project.org)Connecting to server...Connected, waiting for authenticationLogged on(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> Connected, sending welcome message...(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> 220-FileZilla Server version 0.9.31 beta(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> 220 Please visit http://sourceforge.net/projects/filezilla/(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> USER anonymous(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> 331 Password required for anonymous(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> PASS *******************(000002) 5/12/2009 12:49:23 PM - (not logged in) (127.0.0.1)> 530 Login or password incorrect!(000002) 5/12/2009 12:49:25 PM - (not logged in) (127.0.0.1)> disconnected.Retrieving settings, please wait...Done retrieving settingsRetrieving settings, please wait...Done retrieving settingsand need to go to 127.0.0.1i´m thinking remove everything other than what is enclosed by parenthesiswhich will give(000002)|(not logged in)|(127.0.0.1)i´m thinking filter to *.*.*.*to get (127.0.0.1), and trim 1 to the left and right Edited May 12, 2009 by josheee12 Link to comment Share on other sites More sharing options...
weaponx Posted May 12, 2009 Share Posted May 12, 2009 try this: $string = "15.23.55.67,30.21.34.245,192.168.1.2,45.66.94.100,192.168.1.3,30.21.34.245,127.0.0.1,8.8.8.8,2.2 .2.2,3.3.3.3,2.2.2.2" $a = StringRegExpReplace($string, '192\.168(\.\d{1,3}){2},?|127\.0\.0\.1,?', '') $a = StringRegExpReplace($string, '((\d{1,3}\.?){4}),?(.*?)(\1)', '\3\4') MsgBox(0, '', $a) Couple problems here. The first expression doesn't work when a blacklisted address is at the end of the string, it leaves a comma behind. Also the second expression should be accepting $a as the first parameter lest you want to discard the results from the first expression. Link to comment Share on other sites More sharing options...
weaponx Posted May 12, 2009 Share Posted May 12, 2009 and need to go to 127.0.0.1i´m thinking remove everything other than what is enclosed by parenthesiswhich will give(000002)|(not logged in)|(127.0.0.1)i´m thinking filter to *.*.*.*to get (127.0.0.1), and trim 1 to the left and rightNo please don't do that. Here you just use a regular expression to gather all ip addresses into an array.#297326 Link to comment Share on other sites More sharing options...
oMBRa Posted May 12, 2009 Share Posted May 12, 2009 now it should works properly: $string = "15.23.55.67,30.21.34.245,192.168.1.2,45.66.94.100,192.168.1.3,30.21.34.245,127.0.0.1,8.8.8.8,2.2 .2.2,3.3.3.3,2.2.2.2,192.168.1.3" $a = StringRegExpReplace($string, '192\.168(\.\d{1,3}){2},?|127\.0\.0\.1,?', '') $a = StringRegExpReplace($a, '((\d{1,3}\.?){4}),?(.*?)(\1)|,\z', '\3\4') MsgBox(0, '', $a) Link to comment Share on other sites More sharing options...
oMBRa Posted May 12, 2009 Share Posted May 12, 2009 (edited) what's the problem? btw replace: $a = StringRegExpReplace($a, "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]", '') with: $a = StringRegExpReplace($a,'[a-zA-Z]', '') Edited May 12, 2009 by oMBRa Link to comment Share on other sites More sharing options...
josheee12 Posted May 12, 2009 Author Share Posted May 12, 2009 what's the problem? btw replace: $a = StringRegExpReplace($a, "[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]", '') with: $a = StringRegExpReplace($a,'[a-zA-Z]', '')try it for yourself. i just get unusable output. what i need is to extract text inn the form of *.*.*.* from the server window. Link to comment Share on other sites More sharing options...
oMBRa Posted May 13, 2009 Share Posted May 13, 2009 if u have looked carefully at the code, instead of just copy paste, u could have noticed that this line is corrupted by an emoticon: StringRegExp($sIP, "(?sad.gif\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.)){3}(?:(25[0-5]$|2[0-4]\d$|1\d{2}$|[1-9]\d$|\d$))") do u see that ''sad.gif'' ? I think the right code is:StringRegExp($sIP, "(?:(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.)){3}(?:(25[0-5]$|2[0-4]\d$|1\d{2}$|[1-9]\d$|\d$))") Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now