2017-02-14 82 views
1

我需要匹配一個字符串(讓我們將其命名爲「/ export/home」)以及隨後的所有內容(包括可選新行),直到下一次出現此字符串。RegEx匹配某個字符串,直到下一次出現此字符串

我試過/(/導出/家。\ n)(。) /但它不會正確的一組從字符串匹配的字符串。

例子:

/export/home/bla "123" "bla" test 1 
/export/home/bla "123" "bla" test 2 
/export/home/bla "123" "bla" test 3 
test4 
test5 
/export/home/bla "123" "bla" test 6 
/export/home/bla "123" "bla" test 7 
/export/home/bla "123" "bla" test 8 
test9 
/export/home/bla "123" "bla" test 1 

一切和包括/導出/家,直到下一個/出口/家應該是匹配的。 任何幫助表示讚賞,謝謝提前

回答

1

你可以使用一個tempered greedy token

(?s)/export/home(?:(?!/export/home\b).)* 
       ^^^^^^^^^^^^^^^^^^^^^^^^ 

regex demo

(?s)將使.匹配任何字符,包括換行字符,/export/home將匹配/export/home(?:(?!/export/home).)*將匹配任何字符(.),零次或多次出現(*),該字符不會啓動/export/home文字字符序列。

如果unroll模式,它看起來像

/export/home/[^/]*(?:/(?!export/home/)[^/]*)* 

this demo

+1

謝謝主席先生!現在我正在充分理解這種模式:) – user3322838

+0

很高興爲你工作。如果我的回答對你有幫助,也請考慮積極投票。 –

相關問題