tags:

views:

54

answers:

2

I'm having trouble with an Expect regular expression.

I'm trying to match on this output:

RUC.hg0         :                     6                  +6
ITPOK.hg0       :                     6                  +6
ITUC.hg0        :                     6                  +6
ITPKT.hg0       :                     6                  +6
IT127.hg0       :                     6                  +6
ITBYT.hg0       :                   456                +456
IR127.hg0       :                     6                  +6
IRPKT.hg0       :                     6                  +6
IRUC.hg0        :                     6                  +6
IRPOK.hg0       :                     6                  +6
IRBYT.hg0       :                   456                +456
IRJUNK.hg0      :                     1                  +1

I want to pull out the '6' from the '+6' in the line:

ITPKT.hg0       :                     6                  +6

I'm using this regular expression:

ITPKT.*\+(\[0-9])

But I'm getting an error when I run the script:

couldn't compile regular expression pattern: quantifier operand invalid
    while executing

"expect -re "ITPKT.*\+(\[0-9])" { 
       puts "$expect_out(1, string)";
       set snt $expect_out(1, string); 
       set sent 1;
   }"

I've read that certain characters need to be escaped or Expect will try to evaluate then (hence the '[' being escaped above), and I'm not getting the 'invalid command' error, so I think I've got past that stage.

But now I'm stuck on why this expression won't compile :-/

Failing a direct answer, does anyone know of any Expect regex tools that might help me debug this?

+2  A: 

Turns out I needed to escape the escape in front of the '+'!

The correct expression is:

expect -re "ITPKT.*\\+(\[0-9])" {...}
Ross Johnston
@Ross Johnston: note that if you enclose the expression in curly braces you don't have to escape the escape. The problem here is that with double quotes, the interpreter does substitution before the expect command ever sees it. With curly braces the expression is passed to the expect command verbatim.
Bryan Oakley
Thanks Bryan - I had read that and tried it during my thrashing about stage, but didn't get it to work somehow. I'm guessing I still had something else wrong. I'll try it again next time I'm in the code.
Ross Johnston
+1  A: 

I would stick with the double quotes, I've found that to be simpler because then you can do variable substitution. Anyway, I believe the problem is that the regexp engine is interpreting the "+" as a "one or more" type quantifier but you want the engine to match it literally in the input.

You need two backslashes before the "+" sign. (Simply putting a single backslash before the plus sign quotes it from the Tcl parser so that the regexp engine sees it as a bare "+" sign.)

donlibes