在Tcl中获取包含单词和某些值的行
我有一个多行变量,格式如下在Tcl中获取包含单词和某些值的行,tcl,Tcl,我有一个多行变量,格式如下 edit <> user:admin time:5/22/2021 03:00:AM point: was:3.4 edit <> user:admin time:5/22/2021 04:00:AM registered: was:false 编辑用户:管理员时间:2021年5月22日03:00:AM点:was:3.4 编辑用户:管理员时间:2021年5月22日04:00:AM注册:was:false 让我们假设上述两行在变量a中
edit <> user:admin time:5/22/2021 03:00:AM point: was:3.4
edit <> user:admin time:5/22/2021 04:00:AM registered: was:false
编辑用户:管理员时间:2021年5月22日03:00:AM点:was:3.4
编辑用户:管理员时间:2021年5月22日04:00:AM注册:was:false
让我们假设上述两行在变量a中
set $a "edit <> user:admin time:5/22/2021 03:00:AM point: was:3.4
edit <> user:admin time:5/22/2021 04:00:AM registered: was:false"
set$a“编辑用户:管理员时间:2021年5月22日03:00:AM点:was:3.4
编辑用户:管理员时间:2021年5月22日04:00:AM注册:was:false“
我试图找到包含字符串“edit”、“point”的行,并试图获取其“was:”。
在tcl中有没有更简单的方法来进行匹配并获得结果?在tcl中,您可以借助
-line
选项regexp
:
# I've added a few newlines for clarity only
if {[
regexp -line {^edit\s.*\spoint: (?:was:(\S*))?} $a matchedLine was
]} then {
puts "matched line: $matchedLine"
puts "was: $was"
} else {
puts "no match"
}