我需要匹配一个字符串(让我们将其命名为“/export/home”)及其后面的所有内容(包括可选的换行符),直到该字符串下次出现为止。
我尝试了/(/export/home.\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
所有内容(包括从/export/home 到下一个/export/home 的内容)都应该匹配。 感谢任何帮助,提前致谢
最佳答案
您可以使用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/)[^/]*)*
关于java - 正则表达式匹配某个字符串,直到该字符串下次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42223967/