Regex to capture all file paths that do not contain directories

I am trying to write a regular expression to match a file path with the following characteristics:

  • do not contain /./or/../
  • at least one subdirectory located inside /tmp/media
  • must end in .log

Here is what I have so far:

\/tmp\/media\/(?!.*\.?\.\/)+(?:.*\.log)

Here are my desired results (if the string does not match, I refer to a requirement that fails):

/tmp/media/log.log             //NO-MATCH (2)
/tmp/media/test/log.log        //MATCH
/tmp/media/../log.log          //NO-MATCH (1)
/tmp/media/./log.log           //NO-MATCH (1)
/tmp/media/test/../log.log     //NO-MATCH (1)
/tmp/media/../test/log.log     //NO-MATCH (1)
/tmp/media/.t/log.log          //MATCH
/tmp/media/.../log.log         //MATCH
/tmp/log.log                   //NO-MATCH (2)
/tmp/media/test/log.notlog     //NO-MATCH (3)
/tmp/media/test/./log.log      //NO-MATCH (1)

I read this question and successfully completed part of the answer, but /tmp/media/log.log still fits when I don't want it. I suspect that this is because an empty line between /media/and log.logsomehow satisfies (?!.*\.?\.\/).

- , , ?

+3
3

:

^(?![^.]*\/\.\.?\/)\/tmp\/media\/(?:[^\/]+)*\/\w+\.log$

+1

? , readlink. , /./ /../, - .

+1

. . .. . , :

\/tmp\/media\/[^\/]+\/.*\.log

Not knowing what language you are programming in, I cannot offer a path logging mechanism.

+1
source

Source: https://habr.com/ru/post/1779202/


All Articles