如何将字符串拆分成单词?

时间:2013-03-11 10:59:02

标签: regex tcl expect

vlan_outcome

的内容
show int description | include Vl[0-9]+
Vl1                            admin down     down
Vl150                          up             up

如何将以下正则表达式置于变量中?

var1 = Vl1  var2 = admin down var3 = down
war2 = Vl150 war3 = up        war3 = up

更正确我希望分配发生(var1 = Vl1或war1 = Vl150)只有当他们的状态显示两列时

到目前为止,我正在努力尝试失败:

foreach element [ split $vlan_outcome "\n" ] {
#regexp (Vl\[0-9\]+)(\[\[:blank:\]\]+)(.*)(\[\[:blank:\]\]+)(.*) ignore vlan state1 state2
puts $element
}

1 个答案:

答案 0 :(得分:2)

您的问题应该是如何Splitting a String Into Words

因此,如果您逐行循环数据,只需将该行拆分为带有正则表达式的单词,如下所示:

set data [regexp -inline -all -- {\S+} $line]
if {[lindex $data 1] == "up" && [lindex $data 2] == "up"} {
    set value [lindex $data 0]
}