powershell中的正则表达式扩展

powershell中的正则表达式扩展,powershell,Powershell,我必须迭代一个大的IP循环,在bash中我可以轻松地做到这一点,但在PowerShell中尝试这样做时,我发现我几乎不知道如何做类似的事情 在bash中,我做了以下工作: for ip in 10.98.{7..65}.{0..255}; do echo "some text and a $ip"; done $ipprefix='10.98'; For ($i=7; $i -le 65; $i++) { For ($j=0; $j -le 255; $j++) {

我必须迭代一个大的IP循环,在bash中我可以轻松地做到这一点,但在PowerShell中尝试这样做时,我发现我几乎不知道如何做类似的事情

在bash中,我做了以下工作:

for ip in 10.98.{7..65}.{0..255}; do 
    echo "some text and a $ip";
done
$ipprefix='10.98';
For ($i=7; $i -le 65; $i++) {
    For ($j=0; $j -le 255; $j++) {
        Write-Host "some text and a $ipprefix.$i.$j";
    }
}
但是,在PowerShell中,我做了以下工作:

for ip in 10.98.{7..65}.{0..255}; do 
    echo "some text and a $ip";
done
$ipprefix='10.98';
For ($i=7; $i -le 65; $i++) {
    For ($j=0; $j -le 255; $j++) {
        Write-Host "some text and a $ipprefix.$i.$j";
    }
}

在PowerShell中有更简单的方法来实现吗?

理想情况下,bash代码必须展开两个循环,因此您所拥有的PowerShell基本上是相同的。也就是说,您可以在Powershell中嵌套两个范围以实现相同的效果

例如:

1..3 | % {$c = $_; 4..6 | % {$d = $_; Write-Host "a.b.$c.$d"}}
给出:

a.b.1.4
a.b.1.5
a.b.1.6
a.b.2.4
a.b.2.5
a.b.2.6
a.b.3.4
a.b.3.5
a.b.3.6
由此,您可以适应上述问题

如果需要,您可以将其缩短一点,并失去一些可读性,如:

1..3 | % {$c = $_; 4..6 | % {Write-Host "a.b.$c.$_"}}

我认为您在bash中实际使用的是一个
glob
。我不知道PS globs是否有那么强大,但我想这是你在谷歌上搜索的,而不是“regex”。还有
foreach($7..65中的I){foreach($0..255中的j){Write Host“$I.$j”}
some!谢谢你抽出时间。