我不明白的正则表达式:(我想找出如果路径只包含7位数例如:
C:\Users\3D Objects\1403036 --> the result should be 1403036
C:\Users\358712\1403036 --> the result should be 1403036
等等
我试过:
$FilesPath -match '([\d{1,7}]{7})')
以及
$FilesPath -match '(\d{7})')
型
目前,我正在处理:
$FilesPath = Read-Host -Prompt
if ($Matches[1].Length -eq '7') {
$FolderNumber = $Matches[1]
}
这是不正确的,因为如果路径中包含数字3,则不存在匹配
如果是这种情况:
C:\Users\3D Objects\1403036854 --> More than 7 digits the result should be empty
或
C:\Users\3874113353D Objects\1403036 --> Should return result for 1403036
我没有数组,只想获取是否有正好7位数的数字,如果包含少于或多于7位数,则不需要
1条答案
按热度按时间cetgtptt1#
你是说像这样的东西?
输出:
这一点:
型
直接将匹配项赋给变量
$result
,如果匹配或$null
,则将是匹配的数值。Regex方法.Match()
不填充$matches
数组。使用regex运算符(它填充
$matches
数组),您还可以执行以下操作:正则表达式详细信息: