我有这样的文字:
text =“已发送56个ipv4数据包和20个ipv6数据包”
我要从此模式中使用tcl中的 regexp 提取56和20。
但是我的实现也提取了4(来自ipv4)和6(来自ipv6)。
[regexp -inline -all {\d+} $text]
有人可以在这里帮忙吗?
答案 0 :(得分:2)
当数字未粘贴到字母,数字或下划线时,您可以使用\y\d+\y
或\m\d+\M
将数字作为整个单词进行匹配:
set text {56 ipv4 packets transmitted and 20 ipv6 packets transmitted}
set results [regexp -inline -all {\y\d+\y} $text]
puts $results
# => 56 20
set results2 [regexp -inline -all {\m\d+\M} $text]
puts $results2
# => 56 20
请参见Tcl demo。
请参见Tcl docs:
\m
仅在单词开头匹配
\M
仅在单词结尾处匹配
\y
仅在单词的开头或结尾匹配
答案 1 :(得分:0)
或
debugPrint
set text {56 ipv4 packets transmitted and 20 ipv6 packets transmitted}
set nums [lmap word [split $text] {
if {[string is integer -strict $word]} then {set word} else continue
}]