1. 程式人生 > >Python 正則表示式驗證Windows路徑

Python 正則表示式驗證Windows路徑

1. Dirve letter paths
(?x)\A
[a-zA-Z]:\\                  # Drive
(?:[^\\/:*?"<>|\r\n]+\\)*    # Folder
[^\\/:*?"<>|\r\n]*           # File
\Z

eg.
C:\DELL\nihao.jgp

2. Drive letter and paths
\A
(?:[a-zA-Z]:|\\\\[a-zA-Z0-9_.$\ -]+\\[a-zA-Z0-9_.$\ -]+)\\    # Drive
(?:[^\\/:*?"<>|\r\n]+\\)*                                     # Folder
[^\\/:*?"<>|\r\n]*                                            # File
\Z

eg.
\\server\share\path\projectfile.mpp

3. Drive letter. UNC, and relative paths
\A
(?:(?:[a-zA-Z]:|\\\\[a-zA-Z0-9_.$\ -]+\\[a-zA-Z0-9_.$\ -]+)\\|    # Drive
    \\?[^\\/:*?"<>|\r\n]+\\?)                                     # Relative path
(?:[^\\/:*?"<>|\r\n]+\\)*                                         # Folder
[^\\/:*?"<>|\r\n]*                                                # File
\Z

eg.
\\server\share\path\projectfile.mpp