我尝试在以下示例中使用 $expect_out(1, string) 打印出子模式组 1;但程序没有采用“$expect_out(1, string)”。相反,它打印出 (1,string) (见下面的输出)。
关于如何打印出子模式组 1(即“SunOS”)的任何想法。
我将括号放在搜索模式中以指示第 1 组用于反向引用;和内部调试(在输出中)似乎表明它正确地捕捉到了这个短语,但我不知道如何打印出来。
谢谢,
#!/usr/bin/perl
use Expect;
my $exp = new Expect;
# Begin: 2 lines for debugging only
$exp->exp_internal(1);
$exp->log_file("./expect_log.txt");
# End: 2 lines for debugging only
$exp->spawn("uname -a");
$exp->expect(10, '-re', "^(SunOS).*") or print "\nNot found!\n";
print "$expect_out(1, string)\n";
输出:
unix-machine% test_expect.pl
SunOS unix-machine 5.12 Generic_000000-00 sun4u sparc SUNW,SPARC-Bus
(1, string)
带有内部调试的输出:
86 unix-machine% test_expect.pl
Spawned 'uname -a'
spawn id(3)
Pid: 25613
Tty: /dev/pts/169
at /home/user/PERL/lib/Expect.pm line 181
Expect::spawn('Expect=GLOB(0x2b2040)', 'uname -a') called at test_expect.pl line 10
Starting EXPECT pattern matching...
at /home/user/PERL/lib/Expect.pm line 561
Expect::expect('Expect=GLOB(0x2b2040)', 10, '-re', '^(SunOS).*') called at test_expect.pl line 11
spawn id(3): list of patterns:
#1: -re `^(SunOS).*'
spawn id(3): 是否有'
match:
pattern #1: -re
^(SunOS).*'? 不。
SunOS unix-machine 5.10 Generic_000000-00 sun4u sparc SUNW,SPARC-Bus
spawn id(3): 是否有SunOS unix-machine 5.12 Generic_000000-00 sun4u sparc SUNW,SPARC-Bus\r\n'
match:
pattern #1: -re
^(SunOS).*'? 是的!!匹配字符串前:'
Match string:
SunOS unix-machine 5.12 Generic_000000-00 sun4u sparc SUNW,SPARC-Bus\r' 匹配字符串后:\n'
Matchlist: (
SunOS') (1, string)