UNIX文件路径最正确的正则表达式是什么? [英] What is the most correct regular expression for a UNIX file path?

查看:279
本文介绍了UNIX文件路径最正确的正则表达式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于UNIX文件路径,最正确的正则表达式(regex)是什么?

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

创建一个可以匹配大多数文件的正则表达式非常容易,但是最好的是,它可以检测转义的空格序列,以及通常在UNIX上文件路径中找不到的不寻常字符.

It's pretty easy to make a regular expression that will match most files, but what's the best one, including one that can detect escaped whitespace sequences, and unusual characters you don't usually find in file paths on UNIX.

而且,是否有几种不同编程语言的库函数提供文件路径正则表达式?

Also, are there library functions in several different programming languages that provide a file path regex?

推荐答案

如果您不介意标识路径的误报,那么您只需要确保路径不包含NUL字符即可;允许其他所有内容(尤其是/是名称分隔符).更好的方法是使用适当的文件IO函数来解析给定的路径(例如

If you don't mind false positives for identifying paths, then you really just need to ensure the path doesn't contain a NUL character; everything else is permitted (in particular, / is the name-separator character). The better approach would be to resolve the given path using the appropriate file IO function (e.g. File.exists(), File.getCanonicalFile() in Java).

详细答案:

这都是 操作系统 文件系统 相关.例如,文件系统的维基百科比较指出,除了文件系统施加的限制,

This is both operating system and file system dependent. For example, the Wikipedia comparison of file systems notes that besides the limits imposed by the file system,

MS-DOS,Microsoft Windows和OS/2 禁止字符\ / : ? * " > < |NUL 在文件和目录中 在所有文件系统中都命名. Unices 和Linux禁止字符/ 文件名和目录名中的NULNUL 所有文件系统.

MS-DOS, Microsoft Windows, and OS/2 disallow the characters \ / : ? * " > < | and NUL in file and directory names across all filesystems. Unices and Linux disallow the characters / and NUL in file and directory names across all filesystems.

在Windows中,也不允许以下保留的设备名称作为文件名:

In Windows, the following reserved device names are also not permitted as filenames:

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5,
COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, 
LPT5, LPT6, LPT7, LPT8, LPT9

这篇关于UNIX文件路径最正确的正则表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆