2017-02-14 65 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

很高兴为你工作。如果我的回答对你有帮助,也请考虑积极投票。 –

相关问题