0

您好 frnds m 在这个程序中面临问题..cn 有没有告诉我哪里出错了??m nt 能够验证 255.255.255.255 ip 地址

puts "Enter a valid ip address:"
set string [gets stdin]
if {[regexp {^([1-9]|[1]\d{2}|[2][0-4]\d|25[0-5])((\.\d{0,2}|1\d{2}|2[0-4]\d|25[0-5]){3})$} $string match]} {
    puts $string
} else {
    puts "INVALID-ADDRESS"
}
4

7 回答 7

5

不要重新发明轮子。有许多关于如何验证 IP 地址的工作示例。见这里一些http://wiki.tcl.tk/989

此外,如果您有 TCLlib,您可以使用::ip::version命令,例如

% package require ip
1.2
% ::ip::version 192.168.0.1
4
% ::ip::version www.google.com
-1
% ::ip::version 192.168.0.333
-1
于 2012-05-17T10:06:17.867 回答
2
set ip "1.2.3.4"
if { [ catch[regexp {(\d+)\.(\d+)\.(\d+)\.(\d+)} $ip man]] } {
    set flag 0
}
set cut [split $ip "."]
set flag "" 
if {[llength $cut] == 4} {
    set flag 1
} else {
    set flag 0 
}
if {[lindex $cut 0] == 0} { 
    set flag  0 
}
foreach nut $cut {
    if {[string length $nut] > 3} {
        set flag 0
    }
    if {$nut > 255} {
        set flag 0 
    }
    set a [split $nut ""]
    if { [lindex $a 0] == 0 } {
        set flag 0
    }
    #set int [lappend mat $nut]
}
if {$flag == 0} { 
    puts "Not a valid IP"
} else {
    puts "IP IS VALID:$man"
}
于 2013-07-11T06:54:58.370 回答
1

I am just giving the easiest pattern for beginners understanding.

pattern: /^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/
于 2012-09-04T08:54:35.297 回答
1
proc validateIP {IP} {
    if {[regexp {^(?:(\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))(?:\.((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))){3}$} $IP]} {
        puts "$IP is a valid IP"
    } else {
        puts "INVALID-ADDRESS"
    }
}

您的正则表达式存在一些错误,特别是点的位置仅与第一种可能性分组

于 2012-05-17T15:02:33.687 回答
1
% set ip "192.168.123.23"
192.168.123.23
% regexp {([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)} $ip match a b c d
1
% if {$a <= 255 & $b <= 255 & $c <= 255 & $d <= 255} {
  puts "this is a valid ip address"
  } else {
  puts "this not a valid ip address"
 }

这是一个有效的 IP 地址

于 2012-10-19T10:40:22.987 回答
0
puts "enter your ip address"
set ip [gets stdin]
regexp {([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)} "$ip" match 1st 2nd 3rd 4rth

if {$1st < 256 & $2nd < 256 & $3rd <256 & $4rth<256} { // numbers in range?
  puts "valid IP address"
} else { 
  puts "not valid IP address"
}
于 2012-09-23T07:03:07.760 回答
0
set ip 100.12.230.250

set mat [regexp -nocase {(([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$} $ip match]


if {$mat == 1} {
    puts "Valid Ip"
} else {
   puts "Invalid Ip"
}
于 2019-04-01T16:18:04.140 回答