TCL:每一行的正则表达式应该是什么样子?

TCL: How the regex for every line should look like?

在 TCL 中,在输出中我有这样的东西:

ABBAA 1 BAABA 1 DNS3 0 0 200 300 400 500 0 0
ABBAA 1 BAABA 1 DNS1 0 0 200 300 400 500 0 0
ABBAA 1 BAABA 1 DNS7 0 0 200 300 400 500 0 0
ABBAB 1 BAABB 1 DNS5 0 0 200 300 400 500 0 0
ABBAB 1 BAABB 1 DNS3 0 0 200 300 400 500 0 0

我想按第四列升序对这个 table 相似的数据集进行排序(因此第一列将与 DNS1UP1 排在一起,然后是 DNS2UP2 等)我发现正则表达式将是最简单的方法,方法是寻找其中包含 "DNS.." 的字符串。但是我的方法并不完全像我想的那样工作,因为它只匹配一行或根本不匹配任何行。

我的方法:

regexp "ABB.*DNS1.*?\N" 
ABB - match beginning of new line
.* - every character between ABB and DNS..
DNS1 - match the main looking for word
.* - every character between DNS... and new line symbol
?\n - non-greedy occurence of new line

我哪里错了?

如果您有这样一个常规格式的行列表,您可以 lsort 它们......使用正确的选项。特别是,-dictionary 适用于混合 text/numbers,而 -index 4 可让您选择作为排序依据的列。

set sortedLines [lsort -index 4 -dictionary $unsortedLines]

regexp 唯一可能的合理用途是为排序准备数据,但您提供的字符串已经可以排序(假设您已经完成 split $data "\n"在它上面实际将其转换为 列表 行,而不仅仅是使用一个大的 ol' 字符串)。