2012-05-05 37 views
2

我正在爲我的應用程序在DejaGNU編寫單元測試,而且我無法匹配多聚苯胺輸出。Dejagnu /期望多行正則表達式

當我從輸出中刪除換行符\n時,我的規則匹配得很好。例如,

輸出:0 123 0 123

expect { 
    -re "^0 \\d+ 0 \\d+$" {pass "$test_name"} 
} 

我想有輸出這樣的:

0 123 
0 123 

我已經試過everyting

"^0 \\d+\\n0 \\d+$" 
"^0 \\d+\n0 \\d+$" 
"^0 \\d+$\\n^0 \\d+$" 
"^0 \\d+$\n^0 \\d+$" 

但他們的工作不。

回答

2

期待確實有一個整體多線模式空間來匹配,但它不一定看到你在想什麼。特別是,新行可能最終由虛擬終端系統轉變。試試這個:

expect { 
    -re {^0 \d+[\r\n]{1,2}0 \d+$} { 
     # Just for debugging (and you might need to write to a log...) 
     binary scan $expect_out(0,string) c* byteList 
     puts [join $byteList ","] 
     # Now do the rest of your code 
     pass "$test_name" 
    } 
} 

注意,我把牙套,而不是雙引號的RE,因爲保持反斜槓的數量下降。
一旦您知道實際上被Expect看到,您可以更直接地對其進行編碼。我通常只是說打印出來而沒有任何混亂,但是由於你正在處理換行符,所以讀取ASCII字節更容易。