2012-12-02 50 views
0

我需要匹配######之間的內容,現在我的正則表達式我想出了「作品」,但是我也在開頭和結尾選擇了新行的內容,我的生活非常複雜。正則表達式在正向lookbehind和正向lookahead之間匹配多行

這是我的正則表達式:

(?<=\#\#\#)[\w\W]*?(?=\#\#\#) 

我想我只能匹配的內容,如果沒有新的線路。

一些示例文本:

### 
stackoverflow.com - penguin;stackoverflow;html;nospin 
http://stackoverflow.com 
Writing the perfect question 
How to ask questions the smart way 
Help vampires 
How to ask a question 
### 
stackoverflow.com - pyramid;stackoverflow;bb;spin 
http://stackoverflow.com 
Writing the perfect question 
How to ask questions the smart way 
Help vampires 
How to ask a question 
### 
stackoverflow;stackoverflow;wiki;nospin 
http://stackoverflow.com 
Writing the perfect question 
How to ask questions the smart way 
Help vampires 
How to ask a question 
### 
stackoverflow;stackoverflow;bb;spin 
http://stackoverflow.com 
Writing the perfect question 
How to ask questions the smart way 
Help vampires 
How to ask a question 
### 
+0

只是從你的比賽中脫掉換行符,是什麼問題? –

+0

我建議這不是正則表達式的好用,因爲它掩蓋了你想要做的事情。正則表達式對於單行文本非常好,但在處理文本塊時會變得非常複雜。你在用什麼語言?將自己讀入數組可能更好,然後當你點擊'###'處理你讀過的行時。 –

+0

當我與它合作時,我在這件事上非常受限,一些名爲ZennoPoster的東西 - 基本上它的低劣的框架。 –

回答

0

你可以嘗試添加新行以超前/斷言背後:

(?<=###\n)[\w\W]*?(?=\r?\n###) 

這應該讓他們走出正則表達式匹配的。

+0

非常感謝,我試圖像(?<= ### \ n)[\ w \ W] *?(?= ### \ n)這樣做,但沒有奏效,你救了我一個很多麻煩。謝謝! –

+3

@CarolRyals你可以通過接受他的回答(勾選答案旁邊的複選標記)來顯示你對凱爾的努力的讚賞。這會給他一些聲望(對你也有一點點),並且還會向未來的訪問者展示你的問題已經解決(以及如何)。 –

+1

@Kyle你應該注意到,這不適用於大量的正則表達式引擎,因爲lookbehind是可變長度的。例如,Java會允許它,因爲在lookbehind內存在有限數量(2)的固定長度的可能性。但是,例如,Perl會扼殺這個。另外,當你試圖獨立於平臺時,使用'\ r?\ n',一路走向意味着'(?:\ n | \ r \ n?)';) –

相關問題