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

前端 未结 6 878
面向向阳花
面向向阳花 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 06:00

    To others who have answered this question, it's important to note that some applications would require a slightly different regex, depending on how escape characters work in the program you're writing. If you were writing a shell, for example, and wanted to have command separated by spaces and other special characters, you would have to modify your regex to only include words with special characters if those characters are escaped.

    So, for example, a valid path would be

      /usr/bin/program\ with\ space 

    as opposed to

      /usr/bin/program with space 

    which would refer to "/usr/bin/program" with arguments "with" and "space"

    A regex for the above example could be "([^\0 ]\|\\ )*"

    The regex that I've been working on is (newline separated for 'readability'):

      "\(                    # Either
           [^\0 !$`&*()+]    # A normal (non-special) character
         \|                  # Or
           \\\(\ |\!|\$|\`|\&|\*|\(|\)|\+\)   # An escaped special character
       \)\+"                   # Repeated >= 1 times
    

    Which translates to

      "\([^\0 !$`&*()+]\|\\\(\ |\!|\$|\`|\&|\*|\(|\)|\+\)\)\+"
    

    Creating your own specific regex should be relatively simple, as well.

提交回复
热议问题