Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用两个不同的正则表达式替换PowerShell中的文本_Regex_Powershell_Obfuscation - Fatal编程技术网

Regex 使用两个不同的正则表达式替换PowerShell中的文本

Regex 使用两个不同的正则表达式替换PowerShell中的文本,regex,powershell,obfuscation,Regex,Powershell,Obfuscation,我正试图通过下面的脚本在每个文件中找到一张美国运通信用卡和一张Visa卡,并将PAN的主要部分替换为每端只留下4颗星星。但现在这一切正常了,我用下面的脚本得到了一个奇怪的结果,而不是仅仅替换VISA卡号的一部分,而是添加了一个替换行的副本。然后,当脚本为美国运通模糊处理运行下一条If语句时,它将按其应该的方式工作 有人知道我的逻辑哪里出了问题吗?非常感谢 $Enhanced_TRXReports = Get-ChildItem $TRXDestinationFilePath -include "

我正试图通过下面的脚本在每个文件中找到一张美国运通信用卡和一张Visa卡,并将PAN的主要部分替换为每端只留下4颗星星。但现在这一切正常了,我用下面的脚本得到了一个奇怪的结果,而不是仅仅替换VISA卡号的一部分,而是添加了一个替换行的副本。然后,当脚本为美国运通模糊处理运行下一条If语句时,它将按其应该的方式工作

有人知道我的逻辑哪里出了问题吗?非常感谢

$Enhanced_TRXReports = Get-ChildItem $TRXDestinationFilePath -include "Payment.txt","InvoiceHeader.txt" -File -Recurse

foreach ($Enhanced_TRXReport in $Enhanced_TRXReports){
$ReportPath = $Enhanced_TRXReport.FullName
$ReportName = $Enhanced_TRXReport.Name
$content = (Get-content $ReportPath) | ForEach {
    if($_ -match ",4[0-9]{15},"){
    $matched = $Matches[0]
    $String = $_
    $firsthalf = $matched.Substring(1,4)
    $secondhalf = $matched.Substring(13,4)
    $final = "," + $firsthalf + '********' + $secondhalf + ","
    [regex]$regex = ",4[0-9]{15},"
    $String -replace $regex,$final 
    }
    if($_ -match ",3[0-9]{14},"){
    $matched = $Matches[0]
    $String = $_
    $firsthalf = $matched.Substring(1,4)
    $secondhalf = $matched.Substring(12,4)
    $final = "," + $firsthalf + '*******' + $secondhalf + ","
    [regex]$regex = ",3[0-9]{14},"
    $String -replace $regex,$final 
}
    else
    {
        $_
    }
    }
Set-Content -Path $ReportPath -Value $content
}

使用纯正则表达式,这里有一个稍微简单的方法来处理它,而不会导致双重处理(主要是因为switch语句是一个显式中断)。我假设这些行上可能有非卡号,虽然我没有这样的理由,但我假设这实际上是一个CSV。此外,美国运通可能是34或37,所以我已经更新了这种情况

#Requires -Version 4

(Get-ChildItem -Path $Path -Include payment.txt,invoiceheader.txt -File -Recurse).ForEach({
    (Get-Content -Path $PSItem.FullName).ForEach({
        ($PSItem -split ',').ForEach({
            switch -Regex ($PSItem) {
                '4\d{15}' {
                    $PSItem -replace '(\d{4})\d{8}(\d{4})', '$1********$2'
                }
                '3[47]\d{13}' {
                    $PSItem -replace '(\d{4})\d{7}(\d{4})', '$1*******$2'
                }
                default {
                    $PSItem
                }
            }
        }) -join ','
    }) | Set-Content -Path $PSItem.FullName
})

因此,我的脚本无法工作的原因是,对于第二个
if
语句,我没有使用
else if
。有一次我使用了
else if
脚本按预期工作。Incorrigible1是正确的。

如果是逗号分隔的,为什么不拆分它们,处理需要的内容,然后重新合并它们?发布一个示例和一个输入文件。另外,请给出一个示例,说明您期望的输出,以及您的代码无法正常工作的具体情况。错误的原因是在一行使用
if()
,在另一行使用
if(){}else{}
,这样您就可能最终导致错误或缺少替换。