RegEx:在JSON中查找多个结果,但仅在特定字段中,忽略其他字段

时间:2017-05-08 10:21:02

标签: json regex powershell

使用RegEx,并在powershell中使用它,我想在json的特定字段中找到所有反斜杠,其他字段值必须被忽略

示例:查找&#34;查找&#34;中的所有后退。字段,但不在此源<替换字段

{
    "Find"   :  "<HintPath>([\.]{2}\\){1}Common\\Debug\\",
    "Replace":  "<HintPath>..\Common\Release\"
},
{
    "Find"   :  "<HintPath>([\.]{2}\\){2}Common\\Debug\\",
    "Replace":  "<HintPath>..\..\Common\Release\"
},
{
    "Find"   :  "<HintPath>([\.]{2}\\){3}Common\\Debug\\",
    "Replace":  "<HintPath>..\..\..\Common\Release\"
},
{
    "Find"   :  "<HintPath>([\.]{2}\\){4}Common\\Debug\\",
    "Replace":  "<HintPath>..\..\..\..\Common\Release\"
},
{
    "Find"   :  "<HintPath>([\.]{2}\\){5}Common\\Debug\\",
    "Replace":  "<HintPath>..\..\..\..\..\Common\Release\"
},
{
    "Find"   :  "<HintPath>([\.]{2}\\){6}Common\\Debug\\",
    "Replace":  "<HintPath>..\..\..\..\..\..\Common\Release\"
}

这对我不起作用:

(?<="Find")\\(?=\",)

1 个答案:

答案 0 :(得分:0)

这适用于powershell($ text保存json字符串):

$find_regex = '\S*["'']Find["''][^"'']*["''](.*)["''],'
$result= Select-String $find_regex -InputObject $text -AllMatches
foreach($match in $result.matches) {
$backslash_escaped = $match.Groups[1].value -replace '\\','\\'
$text = $text -replace [Regex]::Escape($match.Groups[1].value), $backslash_escaped
}