Reputation: 395
I am trying to do a simple comparison in my expect script using regex. But, its not working for some reason.
if { $loadNum == {[0-9]+} } {
Do something
} else {
Exit
}
loadNum is an input which I get from user and I just want to make sure its just a number. But, with the above script, its always going to else case.
Could you please help me in finding what I am doing wrong ?
Upvotes: 0
Views: 3111
Reputation: 659
Why do you compare string with regex? I think you should use command "regexp".
if {[regexp {[0-9]+} $loadNum]} {
Do something
} else {
Exit
}
Upvotes: 2
Reputation: 246837
If you want to check if your string contains a digit:
if {[regexp {\d} $loadNum]}
# or
if {[string match {[0-9]} $loadNum]}
If you want to check if your string is only digits, pick one of
if {[regexp {^\d+$} $loadNum]}
if {![regexp {\D} $loadNum]}
if {![string match {[^0-9]} $loadNum]}
Upvotes: 1