开发者

Expect (the language) "couldn't compile regular expression pattern: quantifier operand invalid" error

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 o开发者_如何学Goperand 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?


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

The correct expression is:

expect -re "ITPKT.*\\+(\[0-9])" {...}


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.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜