Question

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 ?

Was it helpful?

Solution

Why do you compare string with regex? I think you should use command "regexp".

if {[regexp {[0-9]+} $loadNum]} {
  Do something
} else {
  Exit
}

OTHER TIPS

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]}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top