regex - 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
解决方案
如果$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]} {
# ...
}
推荐阅读
- python - 匹配未嵌套的元素(scrapy)
- sql - 使用 CTE 执行插入语句
- java - NullValueCheckStrategy.ALWAYS 问题(mapstruct 1.3.1)
- sql - 在postgresql上获取一行的多个值
- javascript - 从另一个 Web 应用程序打开一个 Web 应用程序 - 服务器端
- django - Django jQuery-File-Upload upload 将 user_id 添加到模型
- java - 我必须在 blueJ 中制作 3 个对象并调用 display(),但我什至无法制作对象。请看代码结果
- javascript - 具有 getter 和 setter 方法的 ES6 类
- .net - 最大年龄的数据注释大于最小年龄
- eclipse - Eclipse(C++),有没有办法禁止编辑具有特定结尾的文件?