fuinei 发表于 2012-10-26 08:57:59

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

本帖最后由 fuinei 于 2012-10-30 09:34 编辑

各位好,请问返回下面字符串中IP地址的正则表达式如何写呢?
Internet address is 192.168.1.100/32
我希望只需返回192.168.1.100即可,谢谢!

xiehuahere 发表于 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)

netegg 发表于 2012-10-26 09:41:18

网上搜搜,有不少ip的正则表达式,auto可以通用

netegg 发表于 2012-10-26 11:08:50

本帖最后由 netegg 于 2012-10-26 11:13 编辑

回复 3# xiehuahere
这个不行,没这么简单,ip的正则很麻烦,需要确定每个字的数字范围

    ^(\d{1,2}|1\d\d|2\d|25)\.(\d{1,2}|1\d\d|2\d|25)\.(\d{1,2}|1\d\d|2\d|25)\.(\d{1,2}|1\d\d|2\d|25)

随便搜了一个,自己看着改吧

xiehuahere 发表于 2012-10-26 11:14:31

回复 4# netegg

这我知道,是没那么容易。
但他这个不需要严格判断,他这个字符串出来的时候应该就是正确的IP,楼主要的应该就只是简单的字符串提取处理。

xiehuahere 发表于 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):

    ^(?\d\d?|2\d|25)\.(?\d\d?|2\d|25)\.
      (?\d\d?|2\d|25)\.(?\d\d?|2\d|25)$



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

afan 发表于 2012-10-26 13:34:27

([\d.]+)/

xlj310 发表于 2012-10-27 08:48:53

本帖最后由 xlj310 于 2012-10-27 08:50 编辑

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

$text="Internet address is 192.168.1.100/32"
$IP=StringRegExp($text,"([\d+.]+)",3)
MsgBox(0,'',$IP)

fuinei 发表于 2012-10-27 17:17:51

谢谢各位的指导,IP地址是3G拨号后返回的IP,使用命令返回的文本文件中有正确的IP地址,经测试xiehuahere兄,Afan版主和xlj310兄给出的正则都可以在字符串中返回IP地址,谢谢!

sleep365 发表于 2012-10-27 20:49:28

A版的最简单实用,速度最快。

fuinei 发表于 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)

afan 发表于 2012-10-29 19:53:42

MsgBox(0,'',$IP)
呵呵

afan 发表于 2012-10-29 19:59:13

(\d+\.[\d.]+)/

fuinei 发表于 2012-10-30 09:33:52

谢谢Afan版主的指导,问题已解决,再次感谢!

muxingwan 发表于 2012-12-3 10:42:04

回复 13# afan


    看了不少你写的正则,发现你很喜欢用[]代替(),本人刚刚接触正则,请教是一下,有什么特殊的含义吗?
页: [1] 2
查看完整版本: [已解决]使用正则表达式返回字符串中IP地址的问题