找回密码
 加入
搜索
查看: 7097|回复: 15

[AU3基础] [已解决]使用正则表达式返回字符串中IP地址的问题

 火.. [复制链接]
发表于 2012-10-26 08:57:59 | 显示全部楼层 |阅读模式
本帖最后由 fuinei 于 2012-10-30 09:34 编辑

各位好,请问返回下面字符串中IP地址的正则表达式如何写呢?
Internet address is 192.168.1.100/32
我希望只需返回192.168.1.100即可,谢谢!
发表于 2012-10-26 11:05:39 | 显示全部楼层
回复 1# fuinei

$oIP = ""
$str = "Internet address is 192.168.1.100/32"
If StringRegExp($str, "^Internet address is .*$") Then
        $oIP = StringRegExpReplace($str, "[^\d]*(\d+.\d+.\d+.\d+).*", '$1')
EndIf
MsgBox(0, "IP address", $oIP)
发表于 2012-10-26 09:41:18 | 显示全部楼层
网上搜搜,有不少ip的正则表达式,auto可以通用
发表于 2012-10-26 11:08:50 | 显示全部楼层
本帖最后由 netegg 于 2012-10-26 11:13 编辑

回复 3# xiehuahere
这个不行,没这么简单,ip的正则很麻烦,需要确定每个字的数字范围
    ^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])

 
随便搜了一个,自己看着改吧
发表于 2012-10-26 11:14:31 | 显示全部楼层
回复 4# netegg

这我知道,是没那么容易。
但他这个不需要严格判断,他这个字符串出来的时候应该就是正确的IP,楼主要的应该就只是简单的字符串提取处理。
发表于 2012-10-26 11:20:59 | 显示全部楼层
Now that we have a subexpression to match a single number from 0 through 255, we can wrap it in parentheses and insert it in place of each \d{1,3} in the earlier regex. This gives us (broken across lines to fit the width of the page):

    ^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.
        ([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$



Quite a mouthful! Was it worth the trouble? You have to decide for yourself based upon your own needs. It matches only syntactically correct IP addresses, but it can still match semantically incorrect ones, such as 0.0.0.0 (invalid because all the digits are zero). With lookahead (☞133), you can disallow that specific case by putting (?!0+\.0+\.0+\.0+$) after ^, but at some point, you have to decide when being too specific causes the cost/benefit ratio to suffer from diminishing returns. Sometimes it's better to take some of the work out of the regex. For example, if you go back to ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ and wrap each component in parentheses to stuff the numbers into the program's version of $1, $2, $3, and $4, you can then validate them by other programming constructs.


参考:Mastering.Regular.Expressions.3rd.Edition.Aug.2006.chm 5.2节

http://bbs.chinaunix.net/thread-2123662-2-1.html
发表于 2012-10-26 13:34:27 | 显示全部楼层
([\d.]+)/
发表于 2012-10-27 08:48:53 | 显示全部楼层
本帖最后由 xlj310 于 2012-10-27 08:50 编辑

32位掩码? 你是拨号上网的吗?
我把A版的正则补全为代码……呵呵~ 这个比较简单,其实我也想得到,为了避免被疑为抄袭,所以我说引用A版的

$text="Internet address is 192.168.1.100/32"
$IP=StringRegExp($text,"([\d+.]+)",3)
MsgBox(0,'',$IP[0])
 楼主| 发表于 2012-10-27 17:17:51 | 显示全部楼层
谢谢各位的指导,IP地址是3G拨号后返回的IP,使用命令返回的文本文件中有正确的IP地址,经测试xiehuahere兄,Afan版主和xlj310兄给出的正则都可以在字符串中返回IP地址,谢谢!
发表于 2012-10-27 20:49:28 | 显示全部楼层
A版的最简单实用,速度最快。
 楼主| 发表于 2012-10-29 19:04:46 | 显示全部楼层
不好意思,从命令中返回的字符串实际如下:
show ip int cellular 0/0/0
Cellular0/0/0 is up, line protocol is up
Internet address is 192.168.1.1/32
Broadcast address is 255.255.255.255
使用代码测试返回值为0,请问下面代码中的正则正确应该如何表示呢?
$text="show ip int cellular 0/0/0" & @CRLF & _
          "Cellular0/0/0 is up, line protocol is up" & @CRLF & _
          "Internet address is 192.168.1.1/32" & @CRLF & _
          "Broadcast address is 255.255.255.255"
$IP=StringRegExp($text,"([\d+.]+)/",3)
MsgBox(0,'',$IP[0])
发表于 2012-10-29 19:53:42 | 显示全部楼层
MsgBox(0,'',$IP[4])
呵呵
发表于 2012-10-29 19:59:13 | 显示全部楼层
(\d+\.[\d.]+)/
 楼主| 发表于 2012-10-30 09:33:52 | 显示全部楼层
谢谢Afan版主的指导,问题已解决,再次感谢!
发表于 2012-12-3 10:42:04 | 显示全部楼层
回复 13# afan


    看了不少你写的正则,发现你很喜欢用[]代替(),本人刚刚接触正则,请教是一下,有什么特殊的含义吗?
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-11-15 10:24 , Processed in 0.100465 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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