3

これを機能させることができず、その理由がわかりません。行末の数字の間にスペースがある行があります。この行に一致することを期待して、数字とスペースをキャプチャ グループに入れる必要があり、スクリプトの後半でそのキャプチャ グループにアクセスする必要があります。

サンプル行は次のとおりです。これは行です 2 4 5 6

私のexpect -reステートメント:expect -re {^this\sis\sa\sline((\s\d)+$)}

次に、$expect_out(1,string) を使用してキャプチャ グループにアクセスしようとしています。

http://gskinner.com/RegExr/を確認すると、一致グループ 1 は「2 4 5 6」であると表示されているため、パターンが正しく一致していると確信しており、$ に「2 4 5 6」が表示されると予想されます。 expect_out(1,string) ですね。

ただし、expect スクリプトを実行すると、次のように表示されます。

expect: does " \u001b[Js\u001b[Jt\u001b[Jr\u001b[Je\u001b[Ja\u001b[Jm\u001b[Js\u001b[J\r\n\u001b[H\u001b[J\r\n\r\n\r\n\r\this is a line 2 4 5 6\r\n\r\nSelect which option you would    like to change\r\nusing the index number: " (spawn_id exp5) match regular expression "^this\sis\sa\sline((\s\d)+$)"? Gate "this?is?a?line*"? gate=yes re=no
expect: timed out
can't read "expect_out(1,string)": no such element in array
while executing
"set streams $expect_out(1,string)"
(file "check-config" line 35)

"2 4 5 6" (または " 2 4 5 6") を変数に入れて、後で期待スクリプトで使用するにはどうすればよいですか?

また、私はあなたの注意を引いていますが、どういうGate=Yes re=No意味ですか? re=No は正規表現が一致しなかったことを意味すると思いますがGate、Expect の世界では何ですか?

4

1 に答える 1

4

問題は、行アンカー^$正規表現に含めていることです。一致させようとしている文字列は明らかに複数行の文字列ですが、Tcl のデフォルトの一致では改行が通常の文字のように扱われます。もう 1 つの複雑な要因は、Tcl が改行を改行と見なしているにもかかわらず、改行が行末記号として\n使用されていることです。\r\n

正規表現で改行を区別する一致を有効にし、キャリッジ リターンを考慮に入れると、次の正規表現が機能することがあります。(?n)^this\sis\sa\sline((?:\s\d)+)\r?$

テスト:

$ expect <<END
spawn sh -c {echo foo; echo "this is a line 2 4 5 6"; echo bar}
exp_internal 1
expect -re {(?n)^this\sis\sa\sline((?:\s\d)+)\r?$}
END
spawn sh -c echo foo; echo "this is a line 2 4 5 6"; echo bar
Gate keeper glob pattern for '(?n)^this\sis\sa\sline((?:\s\d)+)\r?$' is 'this?is?a?line*'. Activating booster.

expect: does "" (spawn_id exp4) match regular expression "(?n)^this\sis\sa\sline((?:\s\d)+)\r?$"? Gate "this?is?a?line*"? gate=no
foo

expect: does "foo\r\n" (spawn_id exp4) match regular expression "(?n)^this\sis\sa\sline((?:\s\d)+)\r?$"? Gate "this?is?a?line*"? gate=no
this is a line 2 4 5 6
bar

expect: does "foo\r\nthis is a line 2 4 5 6\r\nbar\r\n" (spawn_id exp4) match regular expression "(?n)^this\sis\sa\sline((?:\s\d)+)\r?$"? Gate "this?is?a?line*"? gate=yes re=yes
expect: set expect_out(0,string) "this is a line 2 4 5 6\r"
expect: set expect_out(1,string) " 2 4 5 6"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "foo\r\nthis is a line 2 4 5 6\r"
于 2013-09-11T17:39:30.403 に答える