pattern matching - tcl regex match ip address in a single line -
i trying write regexp check whether ip valid or not.facing issue when give 256 value still matching 2, , reg store value 1 since pattern matched.
set ip "256.256.255.1" set reg [regexp -all{^([1-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([1-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([1-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([1-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])} $ip match ] puts $reg
the unescaped .
match any character, not .
symbol. capturing groups unnecessary since interested in 1 whole match. besides, no need use -all
since want validate single string.
use
set text {256.256.255.1} set pattern {^(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(?:\.(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}$} regexp $pattern $text match if {[info exists match]} { puts $match } else { puts "no match" }
see online tcl demo.
pattern details:
^
- start of string(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
- first octet matching numbers0
255
(?:\.(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}
- 3 occurrences of.
(see\.
) , octet subpattern$
- end of string.
Comments
Post a Comment