Home > Archive > Tcl > June 2007 > Expecting multiple pattern
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Expecting multiple pattern
|
|
|
| I am expecting multiple reponse to a command, and then action
corresponding to some of the pattern is same, i want to combine such
patterns (variables) , so that i dont need to type in the same piece
of code again.
expect {
"qqq" { ............... }
"rrrr" { ..................}
"zzz" or "yyy" {..........}
}
My action corresponding to zzz and yyy is common ( zzz and yyy are in
actual complex pattern in my scripts, this is just an example), so how
can i group them as a single case
| |
| Uwe Klein 2007-06-23, 4:18 am |
| rick wrote:
> I am expecting multiple reponse to a command, and then action
> corresponding to some of the pattern is same, i want to combine such
> patterns (variables) , so that i dont need to type in the same piece
> of code again.
>
> expect {
>
> "qqq" { ............... }
> "rrrr" { ..................}
> "zzz" or "yyy" {..........}
> }
>
> My action corresponding to zzz and yyy is common ( zzz and yyy are in
> actual complex pattern in my scripts, this is just an example), so how
> can i group them as a single case
>
from the expect manpage ( see the failed / invalid password case):
expect {
busy {puts busy\n ; exp_continue}
-re "failed|invalid password" abort
timeout abort
connected
}
The construct that is used in switch like
switch $value -- $pata - $patb $actionscript
does not work.
uwe
| |
|
| On Jun 22, 6:43 pm, Uwe Klein <uwe_klein_habertw...@t-online.de>
wrote:
> rick wrote:
>
>
>
>
> from the expect manpage ( see the failed / invalid password case):
> expect {
> busy {puts busy\n ; exp_continue}
> -re "failed|invalid password" abort
> timeout abort
> connected
> }
>
> The construct that is used in switch like
>
> switch $value -- $pata - $patb $actionscript
>
> does not work.
>
> uwe
Hi Uwe,
expect {
-re "$a | $b" {....}
timeout {...}
}
It is not working for me and taking me to the timeout case.
Also exp_internal is also not showing anything...
I earlier trying using the "switch" kinda approach (-), it cannt find
-, but through exp_internal I found that it matched one of the two
patterns.
| |
|
|
exp_internal shows that , it is taking "$a | $b" as a single pattern
and hence is not able to match.
| |
| Uwe Klein 2007-06-23, 4:18 am |
| rick wrote:
> On Jun 22, 6:43 pm, Uwe Klein <uwe_klein_habertw...@t-online.de>
> wrote:
>
>
>
>
> Hi Uwe,
>
> expect {
> -re "$a | $b" {....}
> timeout {...}
> }
>
> It is not working for me and taking me to the timeout case.
> Also exp_internal is also not showing anything...
>
> I earlier trying using the "switch" kinda approach (-), it cannt find
> -, but through exp_internal I found that it matched one of the two
> patterns.
>
If you use the single argument style for expect you do not get
variable substitution in the patterns ( and you added spaces in the RE
aswell.
Try:
set pata ABCDEF
set patb VWXYZ
expect \
-re "$pata|$patb" {
puts stderr MATCHED:a|b
} timeout {
puts stderr TIMEOUT
}
G!
uwe
| |
| Glenn Jackman 2007-06-23, 4:18 am |
| At 2007-06-22 09:24AM, "rick" wrote:
> I am expecting multiple reponse to a command, and then action
> corresponding to some of the pattern is same, i want to combine such
> patterns (variables) , so that i dont need to type in the same piece
> of code again.
>
> expect {
>
> "qqq" { ............... }
> "rrrr" { ..................}
> "zzz" or "yyy" {..........}
> }
>
> My action corresponding to zzz and yyy is common ( zzz and yyy are in
> actual complex pattern in my scripts, this is just an example), so how
> can i group them as a single case
Another approach is to write that common code as a proc, so:
proc ZorY {} {
blah
}
expect {
qqq {do qqq stuff}
zzz {ZorY}
yyy {ZorY}
}
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
| |
|
|
Uwe,
I tried using your approach , but still no success.
Actually, substitution is occuring ; but after substituting $pata,
$patb it is taking "ABCDEF|VWXYZ" as single pattern, and hence not
able to match.
Glenn,
Yes, a proc can be written, but I was looking something like the way
Uwe suggested, which is somehow not working for me
| |
|
|
| Bezoar 2007-06-25, 4:23 am |
| On Jun 22, 11:08 am, rick <vagabond_maver...@yahoo.com> wrote:
> Uwe,
>
> I tried using your approach , but still no success.
> Actually, substitution is occuring ; but after substituting $pata,
> $patb it is taking "ABCDEF|VWXYZ" as single pattern, and hence not
> able to match.
>
> Glenn,
>
> Yes, a proc can be written, but I was looking something like the way
> Uwe suggested, which is somehow not working for me
Remember that when you are evaluating a regexp ( ie enclosing in "" )
that you must escape all special characters
as many times as necessary.
For instance, if you expect a string like this from your application:
the [man] was in "trouble".
The output will match regexp ( note) the braces:
{the \[man\] was in \"trouble\"\.}
but not
"the \[man\] was in \"trouble\"\.".
So if you surround your regexp with quotes you must excape twice or
ensure that you use braces
to prevent evaluation. See the following interaction .
% set buffer "the \[man\] was in \"trouble\"."
the [man] was in "trouble".
% regexp {the \[man\] was in \"trouble\"\.} $buffer
1
% set reg {the \[man\] was in \"trouble\"\.}
the \[man\] was in \"trouble\"\.
% regexp "$reg" $buffer
1
% set reg "the \[man\] was in \"trouble\"\."
the [man] was in "trouble".
% regexp "$reg" $buffer
0
% set reg "the \\\[man\\\] was in \\\"trouble\\\"\\."
the \[man\] was in \"trouble\"\.
% regexp "$reg" $buffer
1
%
Carl
|
|
|
|
|