正则表达式匹配特定的目录路径,但不匹配其他路径

时间:2017-02-03 02:42:07

标签: regex

我有一个文件包含很多行。

例如:

HomeFolder=file:/user/local/home/joe/
HomeFolder=file:/user/local/home/max/
HomeFolder=file:/user/local/home/ted/
HomeFolder=file:/user/local/home/
HomeFolder=file:/user/local/
HomeFolder=file:/user/
HomeFolder=file:/

我想要的只是匹配以下几行:

HomeFolder=file:/user/local/home/
HomeFolder=file:/user/local/
HomeFolder=file:/user/
HomeFolder=file:/

请帮我提出能够达到上述情况的正则表达式。

1 个答案:

答案 0 :(得分:1)

This regex将只捕获您想要的路径,并使用negative lookahead仅捕获home/之后是否还有子目录

^(HomeFolder=file:\/(?:user\/(?:local\/(?:home\/)?)?)?)(?!\w+\/)