找回密码
 加入
搜索
查看: 1764|回复: 6

如何过滤ip的字符串?

[复制链接]
发表于 2009-6-12 14:07:23 | 显示全部楼层 |阅读模式
本帖最后由 ac5474012 于 2009-6-14 14:00 编辑

我有一批字符串,有的是域名,比如 http://www.apache.org/ 。有的是ip比如 60.28.232.148 我想把ip的字符串过滤出来。请问如何做
 楼主| 发表于 2009-6-12 14:21:55 | 显示全部楼层
本帖最后由 ac5474012 于 2009-6-12 14:40 编辑

这是20行的例子

我要过滤的是1244606671这个,把不是ip格式的全部过滤掉。

第1行,读取的信息 1244606671=读取的所有信息 TIMESTAMP 1244606671 Wed Jun 10 12:04:31 2009
第2行,读取的信息 1244606678=读取的所有信息 TIMESTAMP 1244606678 Wed Jun 10 12:04:38 2009
第3行,读取的信息 60.29.156.14=读取的所有信息 202.99.96.68 60.29.156.14 2591 9 * *
第4行,读取的信息 218.59.144.47=读取的所有信息 192.168.1.65 218.59.144.47 77 1 xia.liu *
第5行,读取的信息 60.29.156.10=读取的所有信息 192.168.1.52 60.29.156.10 133 2 hong.qi *
第6行,读取的信息 192.168.2.31=读取的所有信息 192.168.1.56 192.168.2.31 174 3 yufang.shi *
第7行,读取的信息 119.42.245.28=读取的所有信息 192.168.1.18 119.42.245.28 80 2 * *
第8行,读取的信息 61.156.39.47=读取的所有信息 192.168.1.51 61.156.39.47 651 6 rui.huang *
第9行,读取的信息 192.168.1.51=读取的所有信息 211.100.19.172 192.168.1.51 3296 6 * rui.huang
第10行,读取的信息 60.29.156.11=读取的所有信息 192.168.1.18 60.29.156.11 723 6 * *
第11行,读取的信息 http://post.soso.com/=读取的所有信息 192.168.1.64 http://post.soso.com/ 1437 8 huaying.zhao *
第12行,读取的信息 192.168.1.65=读取的所有信息 http://www.youdao.com/ 192.168.1.65 1885 4 * xia.liu
第13行,读取的信息 http://www.pilottravelcenters.com/Cards/Fleet_Card.aspx=读取的所有信息 192.168.1.65 http://www.pilottravelcenters.com/Cards/Fleet_Card.aspx 171000 127 xia.liu *
第14行,读取的信息 60.28.232.148=读取的所有信息 192.168.1.64 60.28.232.148 1009 5 huaying.zhao *
第15行,读取的信息 221.196.55.165=读取的所有信息 192.168.1.18 221.196.55.165 6482 12 * *
第16行,读取的信息 www.google.com=读取的所有信息 192.168.1.51 www.google.com 729 4 rui.huang *
第17行,读取的信息 60.29.156.10=读取的所有信息 192.168.1.51 60.29.156.10 133 2 rui.huang *
第18行,读取的信息 192.168.1.1=读取的所有信息 192.168.1.64 192.168.1.1 1167 21 huaying.zhao *
第19行,读取的信息 192.168.1.52=读取的所有信息 60.29.156.10 192.168.1.52 82 1 * hong.qi
第20行,读取的信息 61.135.216.234=读取的所有信息 192.168.1.65 61.135.216.234 642 3 xia.liu *
 楼主| 发表于 2009-6-12 15:15:58 | 显示全部楼层
也就是说我可以提取一下这3类
1244606671
60.29.156.14
http://post.soso.com/
而我要把第一种和第三种过滤掉,只留下第二种ip地址。
发表于 2009-6-12 15:49:58 | 显示全部楼层
用 IP 地址专用的 正则表达式!
多用用搜索!
详见:http://www.autoitx.com/forum.php ... 2%B1%ED%B4%EF%CA%BD
((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)

评分

参与人数 1金钱 +3 收起 理由
ac5474012 + 3 多谢提供,我先看看

查看全部评分

发表于 2009-6-12 16:34:37 | 显示全部楼层
楼上方法是可以匹配ip
检测一下就知道了

不过是匹配了所有的ip了
和楼主需要得到的结果的还有点差别
因为他只是想取得‘读取的信息 1244606671=读取的所有信息’之间的ip
还需要加以判断一下
#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBoxEx.au3>

#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("正则判断", 321, 228, -1,-1,$WS_OVERLAPPEDWINDOW, 0x00000018)
$Edit1 = GUICtrlCreateEdit("", 16, 8, 289, 113)
GUICtrlSetData($Edit1, '')
$Combo1 = GUICtrlCreateCombo("获取参数", 176, 168, 97, 25)
$Button1 = GUICtrlCreateButton("运行正则", 192, 136, 65, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
$a=GUICtrlRead($Edit1)
$b = stringRegExp($a,'读取的信息\s(.+).+读取',3);获得目标字符段
for $i = 0 to UBound($b) - 1
If StringInStr ($b[$i], "/") Then ContinueLoop;排除网址
If StringInStr ($b[$i], "www") Then ContinueLoop;排除网址
If Not StringInStr ($b[$i], ".") Then ContinueLoop;排除非ip字符
GUICtrlSetData ($Combo1,$b[$i])

Next

EndSwitch
WEnd

效果:


再送楼主一个正则表达式测试脚本
用来测试表达式是否有效
非常有用的一个小工具哦

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +3 贡献 +3 收起 理由
ac5474012 + 3 + 3 你真强大,我没钱了嘻嘻嘻

查看全部评分

 楼主| 发表于 2009-6-12 17:05:07 | 显示全部楼层
不过我最后用的这个
;~ 打开一个文件已共读写-----------------------------------------------------------------
$rzwj = FileOpen("测试日志.jun", 0)

; 检查打开的文件是否可读
If $rzwj = -1 Then
        MsgBox(0, "错误", "不能打开文件.")
        Exit
EndIf

Local $h = 1 ;确定行数


                        #include <array.au3>
#include <inet.au3>
While 1
        $rzwjh = FileReadLine($rzwj)
        If @error = -1 Then ExitLoop
        ;MsgBox(0, "读取的行:", $rzwjh)
        $rzwjfg = StringSplit($rzwjh, ' ', 1)
        ;MsgBox(0, "读取的行的第二个字符串:", $rzwjfg[2])
        Local $str_t2 = StringSplit($rzwjfg[2], ".")
        If $str_t2[0] = 4 Then
                ;Local $tmp1 = StringReplace($str_t2[1], "*", ""), $tmp2 = StringReplace($str_t2[2], "*", ""), $tmp3 = StringReplace($str_t2[3], "*", ""), $tmp4 = StringReplace($str_t2[4], "*", "")
                If StringLen($str_t2[1]) <= 3 And StringLen($str_t2[2]) <= 3 And StringLen($str_t2[3]) <= 3 And StringLen($str_t2[4]) <= 3 Then
                        ;MsgBox(0,"",$rzwjfg[2])
                        IniWrite(@ScriptDir & "\日志过滤.ini", "过滤的结果", "第" & $h & "行", "过滤后为 " & $rzwjfg[2])
;~ 进行分析--------------------------------------------------------

Local $aResult, $sIp
TCPStartup()
$sIp = TCPNameToIP($rzwjfg[2])
$aResult = _TCPIpToName ($sIp, 1)
If @error Then
    ;MsgBox(0, "_TCPIpToName()", "@error = " & @error & @LF & "@extended = " & @extended)
        IniWrite(@ScriptDir & "\分析日志.ini", "分析结果", "第" & $h & "行", "分析后为 ip:" & $rzwjfg[2]&"错误报告:"&@error)
Else
;~     _ArrayDisplay($aResult, "Local Hostname(s)")
;~     MsgBox(0,"1",$aResult[0])
;~         MsgBox(0,"2",$aResult[1])
;~         MsgBox(0,"3",$aResult[2])
;~         MsgBox(0,"4",$aResult[3])
IniWrite(@ScriptDir & "\分析日志.ini", "分析结果", "第" & $h & "行", "分析后为 " & $aResult[1])
EndIf
                       
                EndIf
        EndIf
;~         完成分析-----------------------------------------------------------------------
        IniWrite(@ScriptDir & "\日志中的第二个字符串.ini", "第二个字符串", "第" & $h & "行,读取的信息 " & $rzwjfg[2], "读取的所有信息 " & $rzwjh)
        $h = $h + 1

WEnd




FileClose($rzwj)
;~ ----------------------------------------------------------------------------------------
发表于 2009-6-12 17:10:42 | 显示全部楼层
呵呵条条大路通罗马
只要达到目的就行了
au3字符串控制就是要运用灵活一点
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-13 08:37 , Processed in 0.082491 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表