Question

I need to match the expect for the following output

1/ 5  SOMETEXT      Active

I am using the following code

expect -re "1\/\s+(\d)\s+SOMETEXT\s+Active"
send "\r"
puts $expect_out(0,string)

Here, I am matching the digit '5' from the input text.

But, with this code, expect is not able to find the result from the input.

Était-ce utile?

La solution 2

You want expect_out(1,string) -- that is the 1st captured group
expect_out(0,string) contains the portion of the text that matches the regex

$ expect
expect1.1> spawn sh -c {echo '1/ 5  SOMETEXT      Active'}
spawn sh -c echo '1/ 5  SOMETEXT      Active'
4093
expect1.2> expect -re {1/\s+(\d+)\s+SOMETEXT\s+Active}
1/ 5  SOMETEXT      Active
expect1.3> puts $expect_out(1,string)
5
expect1.4> puts $expect_out(0,string)
1/ 5  SOMETEXT      Active

Autres conseils

You have a problem in this code; there are backslashes in there that need quoting so they go to the RE engine. Try one of these:

expect -re "1/\\s+(\\d)\\s+SOMETEXT\\s+Active"
expect -re {1/\s+(\d)\s+SOMETEXT\s+Active}

Note also that / is not special at all to Tcl's RE engine or to Tcl; it's just an ordinary character. You never need to quote it in itself (unlike with \ where you need to be careful).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top