期望中的正则表达式 [英] Regular expressions in expect
问题描述
我刚刚开始学习期望脚本.我一直在尝试从输出中提取以下内容:
I just started learning expect scripting.I have been trying to extract the following from my output:
core.4046140998.01.10.133211
使用以下命令使用期望脚本.有人可以告诉我我要去哪里了吗?我想将整个字符串ie(core.4046140998.01.10.133211 *)存储在变量中并执行一些操作
with an expect script using the following command.can somebody please tell me where I am going wrong?I want to get to store this entire string i.e.( core.4046140998.01.10.133211*) in a variable and perform some action with it.
expect -re {^(core)\.*} {puts $expect_out(0,string)}
我是否需要导入任何软件包才能使它正常工作?
Do i have to import any packages in expect to make this work?
推荐答案
由于这是预期的,因此"core"可能出现在行的开头,而不是出现在输入字符串的开头 .演示:
Since this is expect, "core" may appear at the beginning of a line, but not at the beginning of the input string. To demonstrate:
$ expect
expect1.1> spawn sh
spawn sh
8043
expect1.2> send "echo core.1234\r"
expect1.3> exp_internal 1
expect1.4> expect -re {^core.*}
Gate keeper glob pattern for '^core.*' is 'core*'. Activating booster.
expect: does "" (spawn_id exp6) match regular expression "^core.*"? Gate "core*"? gate=no
sh-4.3$ echo core.1234
core.1234
sh-4.3$
expect: does "sh-4.3$ echo core.1234\r\ncore.1234\r\nsh-4.3$ " (spawn_id exp6) match regular expression "^core.*"? Gate "core*"? gate=yes re=no
expect: timed out
expect1.5> expect -re {(?n)^core.*}
Gate keeper glob pattern for '(?n)^core.*' is 'core*'. Activating booster.
expect: does "sh-4.3$ echo core.1234\r\ncore.1234\r\nsh-4.3$ " (spawn_id exp6) match regular expression "(?n)^core.*"? Gate "core*"? gate=yes re=yes
expect: set expect_out(0,string) "core.1234\r"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "sh-4.3$ echo core.1234\r\ncore.1234\r"
expect1.6> puts ">>>$expect_out(0,string)<<<"
<<<core.1234
注意事项:
- 期望
-re {^core.*}
不匹配.我们看到超时"消息 -
请注意我们要匹配的内容:
- expecting
-re {^core.*}
did not match. We see the "timed out" message note what we're attempting to match:
expect: does "sh-4.3$ echo core.1234\r\ncore.1234\r\nsh-4.3$ " (spawn_id exp6) match regular expression "^core.*"? Gate "core*"? gate=yes re=no
# ............^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
它以我发送的命令开头,因此使用普通"锚将不起作用
It starts with the command I sent, so using a "normal" anchor won't work
我接下来要期待的是-re {(?n)^core.*}
. 匹配.
the next thing I expect is -re {(?n)^core.*}
. This does match.
-
(?n)
是很少使用的 Tcl正则表达式告诉正则表达式引擎我们需要换行敏感"匹配的指令. - 对换行符敏感的匹配意味着
.
将不匹配换行符,并且(在此处更相关)^
可以在多行字符串中的换行符之后立即匹配(与$
类似)
- the
(?n)
is a little-used Tcl regex instruction that tells the regex engine we want "newline-sensitive" matching. - newline-sensitive matching means that
.
will not match a newline and (more relevant here) that^
can match immediately after a newline within a multi-line string (similarly for$
)
这里的外卖课程是:
- 很难提取命令的输出,因为提示和发送的命令可能会妨碍您.
- 使用期望调试"来查看为什么您的模式不匹配.
这篇关于期望中的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!