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

时间:2022-09-13 16:49:08

With RegEx, AND using it in powershell, I want to find all backslashes in a specific field of a json, other field values must be ignored

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

Example: Find all backslases in "Find" fields, but not in Replace fields inthis source

示例:在“查找”字段中查找所有后退,但不在此源中的替换字段中查找

{
    "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\"
}

This did not work for me:

这不适合我:

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

1 个解决方案

#1


0  

this works in powershell ($text holds the json string):

这适用于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
}

#1


0  

this works in powershell ($text holds the json string):

这适用于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
}