开发者

Regex to match even hex numbers in Tcl?

开发者 https://www.devze.com 2023-02-20 22:27 出处:网络
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:

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

精彩评论

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