Reputation:
I have a question about regular expressions in expect,
I use the following expression:
expect {
-re "PLAYER: (RON)_(\[0-9]*)"
###do something using switch
}
to match the following format of output "PLAYER:RON_90"
, the first part of the output is always the same: "PLAYER:RON_"
, but the second part of it(the name after the first part) is changing alawys, sometimes is PLAYER:RON_90
, sometimes is PLAYER:RON_87
, PLAYER:RON_75
, I want to do different action based on the first number of the second part, for example: if it is PLAYER:RON_second part
(90 to 99), do action 1, if it is PLAYER:RON_second part
(80 to 89),do action 2, if it is PLAYER:RON_second part
(70 to 79), do action 3.
how to achive it? modify the regular expressions? or some other ways? can anyone help?
Upvotes: 3
Views: 22093
Reputation: 30293
From the manpage:
If a process produced the output "abbbcabkkkka\n", the result of:
expect -indices -re "b(b*).*(k+)"
is as if the following statements had executed:
set expect_out(0,start) 1 set expect_out(0,end) 10 set expect_out(0,string) bbbcabkkkk set expect_out(1,start) 2 set expect_out(1,end) 3 set expect_out(1,string) bb set expect_out(2,start) 10 set expect_out(2,end) 10 set expect_out(2,string) k set expect_out(buffer) abbbcabkkkk
So, for the following regular expression...
-re "PLAYER: (RON)_(\[0-9])(\[0-9]+)"
...you could do this:
if {[info exists expect_out(1,string)]} {
switch -- $expect_out(1,string) {
case "9":
// ...
case "8":
// ...
case "7":
// ...
}
}
And similarly you can see the "extra" digits by checking [info exists expect_out(2,string)]
.
Upvotes: 1
Reputation: 40773
How about:
expect {
-re {PLAYER RON_(\d+)} {
}
The notation \d+ means "at least one decimal digit".
UPDATE:
expect -re {PLAYER RON_(\d+)} {
set playerNumber $expect_out(1,string)
set playerGroup [expr {$playerNumber / 10}]
switch -- $playerGroup {
8 { puts "80-89" }
9 { puts "90-99" }
10 { puts "100-109" }
}
}
If we have a match, then the playerNumber will be the number right after the RON_
and playerGroup will be what you are looking for.
Upvotes: 9