Tcl 正则表达式将所有内容提取到列表中

问题描述 投票:0回答:1

我正在尝试收集所有匹配项,然后循环遍历它们以获得正确的结果。当我循环时,我只获得第一个捕获

04:04:01.121

模仿

set fileData "here is more stuf
04:04:01.121 Found Me 1
this is nothing
04:04:01.122 Found Me 2
this is nothing 1
04:04:01.123 Found Me 3
this is nothing 2
04:04:01.124 Found Me 4
this is nothing 3
04:04:01.125 Found Me 5
this is nothing 4
04:04:01.126 Found Me 6"
set testRegEx "(\\d{2}:\\d{2}:\\d{2}.\\d{3}).*Found Me"
set regexList [regexp -nocase -all -inline $testRegEx $fileData]
foreach {whole item} $regexList {
    puts "-----$item"
}
regex tcl
1个回答
0
投票

*
这样的重复运算符是贪婪;他们尽可能匹配。所以你的 RE 匹配开头的
04:04:01.121
和最后一个
Found Me
,其他所有内容都被
.*
消耗。

尝试:

set testRegEx "(\\d{2}:\\d{2}:\\d{2}.\\d{3}) Found Me"

相反。

© www.soinside.com 2019 - 2024. All rights reserved.