EXPECT 脚本中的正则表达式模式匹配
Regex pattern match in EXPECT script
我有一个 EXPECT 脚本来监视 IBMIHS 服务器上的 http pid:
....
send "ps -ef|grep htt|grep start|wc -l \r"
expect {
-re {.*(\d+).*} {
set theNum $expect_out(1,string)
}
}
puts "theNum = $theNum"
if {$theNum > 8} {
puts "it is ok"
} else {
puts "it is not ok"
}
....
send "ps -ef|grep htt|grep start|wc -l \r"
生成:
send: sending "ps -ef|grep htt|grep start|wc -l \r" to { exp5 }
Gate
keeper glob pattern for '.(\d+).' is ''. Not usable, disabling the
performance booster.
expect: does "" (spawn_id exp5) match regular expression ".(\d+)."?
(No Gate, RE only) gate=yes re=no
ps -ef|grep htt|grep start|wc -l
expect: does "ps -ef|grep htt|grep start|wc -l \r\n" (spawn_id exp5)
match regular expression ".(\d+)."? (No Gate, RE only) gate=yes
re=no
11
expect: does "ps -ef|grep htt|grep start|wc -l \r\n11\r\n" (spawn_id
exp5) match regular expression ".(\d+)."? (No Gate, RE only)
gate=yes re=yes
预期:设置expect_out(0,字符串)“ps-ef|grep htt|grep start|wc -l
\r\n11\r\n"
预期:设置 expect_out(1,string) "1"
期望:设置
expect_out(spawn_id) "exp5" 期望:设置 expect_out(缓冲区) "ps
-ef|grep htt|grep start|wc -l \r\n11\r\n"
theNum = 1
不是好的
命令行实际上returns一个数字“11”,但是(\d+)
捕获一个'1' 代替。
提前感谢您的意见。
这是由于前导 .*
的贪婪——因为它吞噬了尽可能多的字符,(\d+)
部分剩余的文本是 last 数字。这是一个演示,其中我还捕获了领先的 ".*":
expect1.11> exp_internal 1
expect1.12> spawn sh -c {echo foo; echo 1234; echo bar}
spawn sh -c echo foo; echo 1234; echo bar
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {78523}
78523
expect1.13> expect -re {(.*)(\d+).*}
Gate keeper glob pattern for '(.*)(\d+).*' is ''. Not usable, disabling the performance booster.
expect: does "" (spawn_id exp10) match regular expression "(.*)(\d+).*"? (No Gate, RE only) gate=yes re=no
foo
1234
bar
expect: does "foo\r\n1234\r\nbar\r\n" (spawn_id exp10) match regular expression "(.*)(\d+).*"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "foo\r\n1234\r\nbar\r\n"
expect: set expect_out(1,string) "foo\r\n123"
expect: set expect_out(2,string) "4"
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) "foo\r\n1234\r\nbar\r\n"
记下“1,string”和“2,string”中存储的内容
解决方案是简化您的正则表达式。如果您只想捕获 first 组数字,请使用
expect -re {\d+}
set theNum $expect_out(0,string)
或者,如果您想捕获第一个数字 那是一行中唯一的字符:
expect -re {\r\n(\d+)\r\n}
set theNum $expect_out(1,string)
这里的一个教训是,您通常不需要在正则表达式模式中使用前导和结尾 .*
通配符:只需关注捕获所需文本所需的内容即可。
我有一个 EXPECT 脚本来监视 IBMIHS 服务器上的 http pid:
....
send "ps -ef|grep htt|grep start|wc -l \r"
expect {
-re {.*(\d+).*} {
set theNum $expect_out(1,string)
}
}
puts "theNum = $theNum"
if {$theNum > 8} {
puts "it is ok"
} else {
puts "it is not ok"
}
....
send "ps -ef|grep htt|grep start|wc -l \r"
生成:
send: sending "ps -ef|grep htt|grep start|wc -l \r" to { exp5 }
Gate keeper glob pattern for '.(\d+).' is ''. Not usable, disabling the performance booster.expect: does "" (spawn_id exp5) match regular expression ".(\d+)."? (No Gate, RE only) gate=yes re=no
ps -ef|grep htt|grep start|wc -lexpect: does "ps -ef|grep htt|grep start|wc -l \r\n" (spawn_id exp5) match regular expression ".(\d+)."? (No Gate, RE only) gate=yes re=no
11expect: does "ps -ef|grep htt|grep start|wc -l \r\n11\r\n" (spawn_id exp5) match regular expression ".(\d+)."? (No Gate, RE only) gate=yes re=yes
预期:设置expect_out(0,字符串)“ps-ef|grep htt|grep start|wc -l \r\n11\r\n"
预期:设置 expect_out(1,string) "1"
期望:设置 expect_out(spawn_id) "exp5" 期望:设置 expect_out(缓冲区) "ps -ef|grep htt|grep start|wc -l \r\n11\r\n"
theNum = 1
不是好的
命令行实际上returns一个数字“11”,但是(\d+)
捕获一个'1' 代替。
提前感谢您的意见。
这是由于前导 .*
的贪婪——因为它吞噬了尽可能多的字符,(\d+)
部分剩余的文本是 last 数字。这是一个演示,其中我还捕获了领先的 ".*":
expect1.11> exp_internal 1
expect1.12> spawn sh -c {echo foo; echo 1234; echo bar}
spawn sh -c echo foo; echo 1234; echo bar
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {78523}
78523
expect1.13> expect -re {(.*)(\d+).*}
Gate keeper glob pattern for '(.*)(\d+).*' is ''. Not usable, disabling the performance booster.
expect: does "" (spawn_id exp10) match regular expression "(.*)(\d+).*"? (No Gate, RE only) gate=yes re=no
foo
1234
bar
expect: does "foo\r\n1234\r\nbar\r\n" (spawn_id exp10) match regular expression "(.*)(\d+).*"? (No Gate, RE only) gate=yes re=yes
expect: set expect_out(0,string) "foo\r\n1234\r\nbar\r\n"
expect: set expect_out(1,string) "foo\r\n123"
expect: set expect_out(2,string) "4"
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) "foo\r\n1234\r\nbar\r\n"
记下“1,string”和“2,string”中存储的内容
解决方案是简化您的正则表达式。如果您只想捕获 first 组数字,请使用
expect -re {\d+}
set theNum $expect_out(0,string)
或者,如果您想捕获第一个数字 那是一行中唯一的字符:
expect -re {\r\n(\d+)\r\n}
set theNum $expect_out(1,string)
这里的一个教训是,您通常不需要在正则表达式模式中使用前导和结尾 .*
通配符:只需关注捕获所需文本所需的内容即可。