使用IP地址TCL的验证(Validation of ip-address using tcl)

2019-10-16 17:35发布

您好frnds我中号面临的问题在这个program..cn任何告诉我在哪里MI犯错? 中号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"
}

Answer 1:

不要重新发明轮子。 有几十个如何验证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


Answer 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"
}


Answer 3:

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"
    }
}

有一些几个错误你的正则表达式,点的专门摆放与第一种可能性分组只



Answer 4:

我只是给初学者理解最简单的模式。

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])$/


Answer 5:

% 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地址



Answer 6:

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"
}


Answer 7:

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"
}


文章来源: Validation of ip-address using tcl
标签: tcl