What is the most correct regular expression for a UNIX file path?

前端 未结 6 904
面向向阳花
面向向阳花 2020-12-03 05:39

What is the most correct regular expression (regex) for a UNIX file path?

For example, to detect something like this:

/usr/lib/libgccpp.so.1.0.2
         


        
6条回答
  •  悲哀的现实
    2020-12-03 05:47

    ^(/)?([^/\0]+(/)?)+$
    

    This will accept every path that is legal in filesystems such as extX, reiserfs.

    It discards only the path names containing the NUL or double (or more) slashes. Everything else according to Unix spec should be legal (I'm suprised with this outcome too).

提交回复
热议问题