首页 > 解决方案 > tcl中的正则表达式太复杂错误

问题描述

对于一个小列表,我还没有看到这个错误。当列表大于 10k 时弹出问题。tcl 中的正则表达式模式的数量有限制吗?

puts "#LEVELSHIFTER_TEMPLATES_LIMITSFILE:$perc_limit(levelshifter_templates)"
puts "#length of templates is :[llength $perc_limit(levelshifter_templates)]"
if { [regexp [join $perc_limit(levelshifter_templates) |] $temp] }

#LEVELSHIFTER_TEMPLATES_LIMITSFILE:HDPELT06_LVLDBUF_CAQDP_1 HDPELT06_LVLDBUF_CAQDPNRBY2_1 HDPELT06_LVLDBUF_CAQDP_1....
#length of templates is :13520
ERROR: couldn't compile regular expression pattern: regular expression is too complex

标签: regextcl

解决方案


如果$temp是一个单词并且您实际上只是在进行文字测试,则应该反转检查。最简单的方法之一可能是:

if {$temp in $perc_limit(levelshifter_templates)} {
    # ...
}

但是,如果您经常这样做(嗯,不止一次,例如 3 或 4 次),那么为此构建字典可能是最好的:

# A one-off cost
foreach key $perc_limit(levelshifter_templates) {
    # Value is arbitrary
    dict set perc_limit_keys $key 1
}

# This is now very cheap
if {[dict exists $perc_limit_keys $temp]} {
    # ...
}

如果您有多个单词$temp,请拆分并检查(使用第二种技术,现在绝对值得)。这是有一个帮助程序可能是一个好计划的地方。

proc anyWordIn {inputString keyDictionary} {
    foreach word [split $inputString] {
        if {[dict exists $keyDictionary $word]} {
            return true
        }
    }
    return false
}

if {[anyWordIn $temp $perc_limit_keys]} {
    # ...
}

推荐阅读