regexperl

Match all files in directory and index files from first level children


I have a list of URIs and one parameter $path. I want to match URIs based on that $path. I want to get all files in $path + all index files from subdirectories but ONLY for the first child. So I don't wanna get all index files from all subdirectories.

Example: $path = /root

/root/index.shtml           - MATCH ME (because file is in $path)
/root/root_one.shtml        - MATCH ME (because file is in $path)
/root/one/index.shtml       - MATCH ME (because im an index-child)
/root/two/index.shtml       - MATCH ME (because im an index-child)
/root/two/test.shtml        - DONT MATCH ME
/root/two/three/index.shtml - DONT MATCH ME
/root/2024/index.shtml      - MATCH ME (because im an index-child)
/root/2024/one.shtml        - DONT MATCH ME

At least based on my knowledge, I think I have to use two regex to match everything.

Match all files within directory: \/root\/(\w+)\.shtml
Match all index.shtml on first childs: \/root\/(\w+)\/{1}index\.shtml

Is there a way to match both in one regex?

Update: I think just when updating, i found a solution: \/root\/(\w+)(\/{1}index\.shtml|\.shtml)


Solution

  • You may use a single regex like this to match all the cases:

    ^\/root\/[^\/]+(?:\/index\.shtml)?$
    

    RegEx Demo

    RegEx Demo: