I have following code:
set variable1 "5"
set variable2 "6-7"
if {[regexp {\m\d+\M} $variable1 matchall]} {
puts "only a number has been provided and that is $matchall"
}
if {[regexp {\m\d+\M} $variable2 matchall]} {
puts "only a number has been provided and that is $matchall"
}
Basically, i want to only match variable1 which is just a number. variable2 does have a hyphen inside which should not match because i am using the \m and \M which are basically doing the same as \b in normal regex which indicates word boundaries. So when i run the code i get:
only a number has been provided and that is 5
only a number has been provided and that is 6
can anyone give me tip in the right direction? I am stuck with TCL 8.3 and cannot upgrade. But regex should basically work the same way as with never versions.
In addition to Barmar’s answer, it is worth noting that you do not need a regular expression here. Just test with the string
command:
string is integer -strict $s
This permits leading and trailing whitespace, though. If that is significant, you can trim it away with
string trim $s
before using it anywhere. To wit:
if {[string is integer -strict $variable1]} {
puts "only a number has been provided and that is [string trim $variable1]"
}