Regex for folder path: only 1 subfolder


I would like to ask for help from people that have more advanced regex understanding than me. I have spent many hours trying stuff and also gone thru the tutorials on youtube and I’m at my wits end because it kind of works but the regex pattern is kind of broad I’m unable to narrow it down

Basically I’m using this regex


so it will consider this valid


also this will be valid


regex demo:

But I find that these are also valid which is not my intention



How can i configure the regex so that only these 2 patterns are valid (root folders are ./apidocs or apidocs)



I’m using this in a python script btw, and found that putting a caret infront in a group does not work. Maybe there is a simpler way to form the regex.

Thank you in advance to anyone that is able to help!

>Solution :

You can use


See the regex demo.


  • ^ – start of string
  • (?:\.\/)? – an optional ./ string
  • apidocs\/static\/ – a apidocs/static/ string
  • [^\/]+ – one or more chars other than /
  • \. – a dot
  • (?:png|jpe?g|gif|pdf)png, jpg or jpeg, gif, pdf
  • $ – end of string

Leave a Reply Cancel reply