路径验证 - 尝试修改我的 RegEx,使其仅匹配包含带扩展名的文件名的路径
Path validation - Trying to modify my RegEx so that it only matches paths that include a filename with an extension
这就是我正在使用的:https://regex101.com/r/BertHu/3/
^(?:(?:[a-z]:|\\[a-z0-9_.$●-]+\[a-z0-9_.$●-]+)\|\?[^\\/:*?"<>|\r\n]+\?)*(?:[^\\/:*?"<>|\r\n]+\)*[^\\/:*?"<>|\r\n]*$
我使用的正则表达式基于this implementation from Oreilly.
这是一个细分(我不得不修复 Oreilly 表达式中的一些未转义字符):
(?:(?:[a-z]:|\\[a-z0-9_.$\●-]+\[a-z0-9_.$\●-]+)\| # Drive
\?[^\\/:*?"<>|\r\n]+\?) # Relative path
(?:[^\\/:*?"<>|\r\n]+\)* # Folder
[^\\/:*?"<>|\r\n]* # File
我在 PowerShell 中实现它,表达式将不区分大小写。
我想修改此表达式,使其仅匹配包含带扩展名的文件的路径。我知道文件可能不包含扩展名 - 我不想匹配这种极端情况。
我希望发生的事情的例子:
C:\Applications\Dev\File.txt
匹配
C:\Applications\Dev\
不匹配
\192.168.0.1\SHARE\my folder\test.exe
匹配
..\..\bin\my_executable.exe
匹配
等等
如果有人能指出我的解决方案,那将非常有帮助!
非常感谢。
编辑:
在考虑了所有人的所有建议后,我最终采用了以下实施方式:
https://github.com/visusys/VSYSUtility/blob/main/Public/Confirm-WindowsPathIsValid.ps1
也许我的一些 PowerShell 书呆子会发现它很有用。 :)
一个实用的解决方案是首先应用您的验证正则表达式 - 如果路径匹配 - 对其调用 System.IO.Path.GetExtension()
.NET API 方法:[1]
- 注意:我没有查看具体细节,但您的正则表达式也匹配格式错误的路径,例如
C:\foo\C:\bar
- follow-up question.
'C:\Applications\Dev\File.txt',
'C:\Applications\Dev\',
'\192.168.0.1\SHARE\my folder\test.exe',
'..\..\bin\my_executable.exe',
'invalid:path' |
ForEach-Object {
$valid = $_ -match '^(?:(?:[a-z]:|\\[a-z0-9_.$●-]+\[a-z0-9_.$●-]+)\|\?[^\\/:*?"<>|\r\n]+\?)*(?:[^\\/:*?"<>|\r\n]+\)*[^\\/:*?"<>|\r\n]*$'
[pscustomobject] @{
Path = $_
Valid = $valid
HasExtension = if ($valid) { '' -ne [IO.Path]::GetExtension($_) }
}
}
输出:
Path Valid HasExtension
---- ----- ------------
C:\Applications\Dev\File.txt True True
C:\Applications\Dev\ True False
\192.168.0.1\SHARE\my folder\test.exe True True
..\..\bin\my_executable.exe True True
invalid:path False
[1] 在 Windows 上,此方法本身执行 有限的 验证:具有 非法字符的路径 例如 "
导致异常,但 格式错误 不会。在类 Unix 平台上,文件系统通常允许 任何 路径中的字符,除了 NUL
,似乎根本不执行任何验证(甚至 NUL
字符不不会导致异常)。
这就是我正在使用的:https://regex101.com/r/BertHu/3/
^(?:(?:[a-z]:|\\[a-z0-9_.$●-]+\[a-z0-9_.$●-]+)\|\?[^\\/:*?"<>|\r\n]+\?)*(?:[^\\/:*?"<>|\r\n]+\)*[^\\/:*?"<>|\r\n]*$
我使用的正则表达式基于this implementation from Oreilly.
这是一个细分(我不得不修复 Oreilly 表达式中的一些未转义字符):
(?:(?:[a-z]:|\\[a-z0-9_.$\●-]+\[a-z0-9_.$\●-]+)\| # Drive
\?[^\\/:*?"<>|\r\n]+\?) # Relative path
(?:[^\\/:*?"<>|\r\n]+\)* # Folder
[^\\/:*?"<>|\r\n]* # File
我在 PowerShell 中实现它,表达式将不区分大小写。
我想修改此表达式,使其仅匹配包含带扩展名的文件的路径。我知道文件可能不包含扩展名 - 我不想匹配这种极端情况。
我希望发生的事情的例子:
C:\Applications\Dev\File.txt
匹配
C:\Applications\Dev\
不匹配
\192.168.0.1\SHARE\my folder\test.exe
匹配
..\..\bin\my_executable.exe
匹配
等等
如果有人能指出我的解决方案,那将非常有帮助!
非常感谢。
编辑:
在考虑了所有人的所有建议后,我最终采用了以下实施方式:
https://github.com/visusys/VSYSUtility/blob/main/Public/Confirm-WindowsPathIsValid.ps1
也许我的一些 PowerShell 书呆子会发现它很有用。 :)
一个实用的解决方案是首先应用您的验证正则表达式 - 如果路径匹配 - 对其调用 System.IO.Path.GetExtension()
.NET API 方法:[1]
- 注意:我没有查看具体细节,但您的正则表达式也匹配格式错误的路径,例如
C:\foo\C:\bar
- follow-up question.
'C:\Applications\Dev\File.txt',
'C:\Applications\Dev\',
'\192.168.0.1\SHARE\my folder\test.exe',
'..\..\bin\my_executable.exe',
'invalid:path' |
ForEach-Object {
$valid = $_ -match '^(?:(?:[a-z]:|\\[a-z0-9_.$●-]+\[a-z0-9_.$●-]+)\|\?[^\\/:*?"<>|\r\n]+\?)*(?:[^\\/:*?"<>|\r\n]+\)*[^\\/:*?"<>|\r\n]*$'
[pscustomobject] @{
Path = $_
Valid = $valid
HasExtension = if ($valid) { '' -ne [IO.Path]::GetExtension($_) }
}
}
输出:
Path Valid HasExtension
---- ----- ------------
C:\Applications\Dev\File.txt True True
C:\Applications\Dev\ True False
\192.168.0.1\SHARE\my folder\test.exe True True
..\..\bin\my_executable.exe True True
invalid:path False
[1] 在 Windows 上,此方法本身执行 有限的 验证:具有 非法字符的路径 例如 "
导致异常,但 格式错误 不会。在类 Unix 平台上,文件系统通常允许 任何 路径中的字符,除了 NUL
,似乎根本不执行任何验证(甚至 NUL
字符不不会导致异常)。