繁体   English   中英

RegEx 匹配某个字符串,直到该字符串下一次出现

[英]RegEx match certain string until next occurence of this string

我需要匹配一个字符串(让我们将其命名为“/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 的所有内容都应该匹配。 任何帮助表示赞赏,提前致谢

您可以使用温和的贪婪令牌

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

查看正则表达式演示

(?s)将使. 匹配任何字符,包括换行符, /export/home将匹配/export/home(?:(?!/export/home).)*将匹配任何字符 ( . ),零次或多次出现 ( * )不启动/export/home文字字符序列。

如果你 展开图案,它看起来像

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

这个演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM