Regex match folder and all subfolders

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.


Use following regex:

/^[^\/]+\/statistics\/?(?:[^\/]+\/?)*$/gm

Demo on regex101.

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

Tags:

Regex