我试图找到一种方法来查找列表中的第一个和最后一个匹配的单词或正则表达式。
类似的东西:
short
答案 0 :(得分:1)
由于正则表达式可能非常慢,因此最好只执行一次正则表达式,而不是多次搜索目标行。
set list "The dog rant to the field by the red house..."
set matches [regexp -inline -all {[Tt]he} $list]
set first [lindex $matches 0]
set last [lindex $matches end]
如果您需要索引到匹配所在的$ list,请使用
-indices
选项。
set matches [regexp -indices -inline -all {[Tt]he} $list]
参考文献:regexp
答案 1 :(得分:1)
set res [lsearch -nocase -all $list the]
set first [lindex $res 0]
set last [lindex $res end]
除非你想特别排除大写h或e(或寻找单词边界),否则这个是等效的,大约快四倍。
答案 2 :(得分:1)
根据实际数据的长度和匹配的分布,在反向列表上进行搜索并转换可能最简单:
'\\old\now\nonexistent\path\to\my\home\documents\\gitignore_global.txt': Permission denied