Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex match all files in directory + index files from first level childs

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

Best regards.

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:

  • ^: Start
  • \/root\/: Match /root/
  • [^\/]+: Match 1+ of any character that is not /
  • (?:\/index\.shtml)?: Match /index.shtml optionally
  • $: End
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading