The issue here is not really the difference between \d
and [0-9]
but rather the way you are handling those two expressions in regex. In Tcl, double quotes allow a first level of substitution. It is only when that level of substitution completes, that the result is passed to the regexp
function.
So with that view, what is actually passed to the regexp
function in the two cases are:
([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})
and
(d{1,3}).(d{1,3}).(d{1,3}).(d{1,3})
Which obviously doesn't match the string you provided. Something like dd.ddaddfdd
would, however (remember that .
matches any single character!).
Remember that backslashes are substituted in Tcl, just like $
or [ ... ]
whenever possible (i.e. when strings are provided without braces). To get a literal backslash, you thus would do (with that current formatting):
([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})
and
(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})
Or to avoid the pain of doing that, use braces:
regexp {([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})} 192.168.1.10
and
regexp {(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})} 192.168.1.10
Something else I would mention here is that you don't need the parentheses for validation if you are not using them for grouping, so this should suffice and improve the speed of your script:
regexp {[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}} 192.168.1.10
And if you want the answer to your title, there is a difference between the \d
and [0-9]
. The most popular question on stackoverflow has been raised from C# but this also applies in Tcl is this one which shows that \d
matches much more than the numbers 0 through 9.