Ashish
Ashish

Reputation: 388

Different version of tcl giving different answers

I am running this function in tcl 8.0 verison.

proc test {} {
    set owner 212549316
    set val [regexp {[0-9]{9}} $owner]
    puts $val
}

The same code in tcl 8.6, the output is 1 but in tcl 8.0 it is 0. I am checking whether the string contains only 9 digits in tcl 8.0.

Any help how to make it work in tcl 8.0 verison.

Upvotes: 3

Views: 90

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627128

In Tcl 8.0, bound (or limiting) quantifiers are not supported.

To match 9 digits in Tcl 8.0, you'd have to repeat the [0-9] 9 times:

set val [regexp {[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]} $owner]

Bound quantifiers are supported starting with Tcl 8.1 with the introduction of advanced regular expression syntax.

Basic regex syntax available in Tcl 8.0 only includes:

.    Matches any character.
*    Matches zero or more instances of the previous pattern item.
+    Matches one or more instances of the previous pattern item.
?    Matches zero or one instances of the previous pattern item.
( )  Groups a subpattern. The repetition and alternation operators apply to the preceding subpattern.
|    Alternation.
[ ]  Delimit a set of characters. Ranges are specified as [x-y]. If the first character in the set is ^, then there is a match if the remaining characters in the set are not present.
^    Anchor the pattern to the beginning of the string. Only when first.
$    Anchor the pattern to the end of the string. Only when last.

See Practical Programming in Tcl and Tk, 3rd Ed. © 1999, Brent Welch, Ch 11, p. 146.

Upvotes: 4

Related Questions