Reputation: 243
I need to write a Regex for a backup exclusion filter to exclude a folder and all of it's subfolders.
I need to match the following
folder1/statistics
folder1/statistics/*
folder2/statistics
folder2/statistics/*
I came up with this Regex which matches the folder statistics, but not the subfolders of the statistics folder.
[^/]+/statistics/
How do I expand this expression to match all subfolders below the statistics folder?
Upvotes: 10
Views: 62056
Reputation: 563
What about using | (or):
'.*/statistics($|/.*)'
Explanation:
.* # any length string
/statistics # statistics directory
($|/.*) # end of string or any string starting with /
It does the job and is not hard to comprehend. Tested with python re module.
Upvotes: 7
Reputation: 92521
Use following regex:
/^[^\/]+\/statistics\/?(?:[^\/]+\/?)*$/gm
Explanation:
/
^ # matches start of line
[^\/]+ # matches any character other than / one or more times
\/statistics # matches /statistics
\/? # optionally matches /
(?: # non-capturing group
[^\/]+ # matches any character other than / one or more times
\/? # optionally matches /
)* # zero or more times
$ # matches end of line
/
g # global flag - matches all
m # multi-line flag - ^ and $ matches start and end of lines
Upvotes: 18