在 Tcl 中获取包含一个单词和一些值的行
Fetch a line containing a word and some value in 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
假设以上两行在变量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"
我试图找到包含字符串“edit”、“point”的行,并试图获取它的“was:”。
在 tcl 中有没有更简单的方法来进行此匹配并获得结果?
在 Tcl 中,您可以借助 regexp
的 -line
选项来完成此操作:
# 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"
}
我在以下格式的变量中有多行。
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
假设以上两行在变量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"
我试图找到包含字符串“edit”、“point”的行,并试图获取它的“was:”。 在 tcl 中有没有更简单的方法来进行此匹配并获得结果?
在 Tcl 中,您可以借助 regexp
的 -line
选项来完成此操作:
# 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"
}