Regex to match even hex numbers in Tcl?
Is there awa开发者_运维知识库y to write a Tcl regex to verify that a string is a hex number that is even?
This tcl code will do the work:
if {[regexp -linestop -nocase {^[\da-f]*[02468ace]$} $input]} {
# Success
} else {
# Fail
}
Note that a, c and e are also even numbers and that you need -nocase to match a-f as well as A-F.
Does it need to be a regex?
proc is_even {n} {expr {($n & 1) == 0}}
if {[is_even 0xdeadbeef]} {puts even} else {puts odd}
精彩评论