【问题标题】:Different version of tcl giving different answers不同版本的tcl给出不同的答案
【发布时间】:2016-05-30 09:50:34
【问题描述】:

我在 tcl 8.0 版本中运行这个功能。

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

tcl 8.6 中的相同代码,输出为1,但在 tcl 8.0 中为0。 我正在检查字符串是否仅包含 tcl 8.0 中的 9 位数字。

任何帮助如何使它在 tcl 8.0 版本中工作。

【问题讨论】:

  • 有趣。 [0-9]\{9\} 是否在 Tcl 8.0 中产生预期结果?从 I see 开始,限制量词已在 8.1 版的 Tcl 中引入。
  • @WiktorStribiżew 这在 tcl 的两个版本(即 8.0 和 8.6)中都给出了 0。
  • 因此,Tcl 8.0 不支持限制(或 绑定)量词

标签: regex tcl tclsh


【解决方案1】:

在 Tcl 8.0 中,绑定(或限制)量词are not supported。

要匹配 Tcl 8.0 中的 9 个数字,您必须重复 [0-9] 9 次:

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

从 Tcl 8.1 开始支持绑定量词,并引入了高级正则表达式语法。

Tcl 8.0 中可用的基本正则表达式语法仅包括:

.    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.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2017-09-19
    相关资源
    最近更新 更多