开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜