在 PowerShell 中过滤以(一个或两个)斜杠开头的字符串
Filter the strings which startswith (one or two) slashes in PowerShell
我试图找出以一个斜杠 (/) 和两个斜杠 (//) 开头的字符串。例如,以下是包含几个字符串的数组:
以下是我正在尝试的代码:
$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
$arraysplit = $array.split(',');
Foreach ($string in $arraysplit)
{
if ($string.StartsWith("/"))
{
Write-Host "$string has one slash."
}
elseif($string.StartsWith("//"))
{
Write-Host "$string has two slashes."
}
else
{
#I want to exit only when below conditions meet
#1. if string doesnot have any slash or
#2. if string has more than two slashes
Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
Exit -1
}
}
我不想写更多的 if
条件来过滤这些东西,但这没有按预期工作。我想我应该改变逻辑来达到要求。有人可以给我建议吗(我正在寻找动态方法)
我会使用正则表达式编写一个 if 测试来匹配任何不是以一个或两个斜杠开头的行。尝试:
$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
Foreach ($string in $array)
{
if ($string -notmatch '^\/{1,2}[^\/]')
{
Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
Exit -1
}
}
只需反转你的测试,因为如果一个单词以 // 开头,它以 /
开头
$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
$arraysplit = $array.split(',');
Foreach ($string in $arraysplit)
{
if ($string.StartsWith("//"))
{
Write-Host "$string has two slash."
}
elseif($string.StartsWith("/"))
{
Write-Host "$string has one slashes."
}
else
{
#I want to exit only when below conditions meet
#1. if string doesnot have any slash or
#2. if string has more than two slashes
Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
Exit -1
}
}
if ($string -match '^/*') { write-host $matches[0].length slashes }
是@wOxxOm 发布的答案。谢谢
我试图找出以一个斜杠 (/) 和两个斜杠 (//) 开头的字符串。例如,以下是包含几个字符串的数组: 以下是我正在尝试的代码:
$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
$arraysplit = $array.split(',');
Foreach ($string in $arraysplit)
{
if ($string.StartsWith("/"))
{
Write-Host "$string has one slash."
}
elseif($string.StartsWith("//"))
{
Write-Host "$string has two slashes."
}
else
{
#I want to exit only when below conditions meet
#1. if string doesnot have any slash or
#2. if string has more than two slashes
Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
Exit -1
}
}
我不想写更多的 if
条件来过滤这些东西,但这没有按预期工作。我想我应该改变逻辑来达到要求。有人可以给我建议吗(我正在寻找动态方法)
我会使用正则表达式编写一个 if 测试来匹配任何不是以一个或两个斜杠开头的行。尝试:
$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
Foreach ($string in $array)
{
if ($string -notmatch '^\/{1,2}[^\/]')
{
Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
Exit -1
}
}
只需反转你的测试,因为如果一个单词以 // 开头,它以 /
开头$array = @("/website","//windows_service","/console_app","//windows","///IIS","test")
$arraysplit = $array.split(',');
Foreach ($string in $arraysplit)
{
if ($string.StartsWith("//"))
{
Write-Host "$string has two slash."
}
elseif($string.StartsWith("/"))
{
Write-Host "$string has one slashes."
}
else
{
#I want to exit only when below conditions meet
#1. if string doesnot have any slash or
#2. if string has more than two slashes
Write-Host "$string has more number of slashes or it doesnot have any slash. Exiting"
Exit -1
}
}
if ($string -match '^/*') { write-host $matches[0].length slashes }
是@wOxxOm 发布的答案。谢谢