jshi510
jshi510

Reputation: 31

Regex to match spcific directory path but not others

I have a file contains many lines.

For example:

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:/

What I want is to match only the following lines:

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

Please help me come up with the regular expression that would achieve the above scenario.

Upvotes: 0

Views: 32

Answers (1)

m_callens
m_callens

Reputation: 6360

This regex will capture only the paths you want, and uses a negative lookahead to only capture if there aren't anymore subdirectories after home/

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

Upvotes: 1

Related Questions