只能在 tcl 正则表达式中匹配子表达式吗?

It is possible to match subexpression only in tcl regexp?

$report 包含以下文本:

// Command : generate report
Report 123
------------------------------
status        Names
------------------------------
Flat     :    Module1
Flat     :    Module2 
------------------------------
Total Flattened = 2

我只想提取模块名称。模块数量未知。如果我能做这样的事情就太好了:

set modules [regexp -all -inline {Flat\s+:\s+(\S+)} $report]

但这会在 $modules 中放入一堆我不关心的额外垃圾。我错过了什么吗?我知道有办法解决这个问题。似乎没有办法关闭匹配完整表达式,这似乎很奇怪。特别是因为有关闭子表达式匹配的语法 (?:).

不,没有办法不获取完整的匹配字符串。

lmap {full capture} $modules {set capture}

为您挑选出捕获的字符串。

# for Tcl 8.5 and earlier
set res [list]
foreach {full capture} $modules {lappend res $capture}

你得到所有这些东西是因为它可能是相关的,如果不是,那么很容易挑选出你想要的部分。

文档:foreach, lmap, set

Getting lmap for Tcl 8.5 and earlier