2017-05-08 45 views
0

用正则表达式,并在PowerShell中使用它,我想找到一个json的特定领域的所有反斜线,其它字段值必须被忽略正则表达式:找到多个结果在JSON,但只能在特定的领域,忽略其他领域

例:查找 “查找” 字段中的所有backslases,但不是在更换领域inthis源

{ 
    "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")\\(?=\",) 
+0

尝试['(?m)(?:\ G(?!\ A)|^\ s *“Find”)\ [^ \\\ n \] * \ ķ\\\'](https://regex101.com/r/pWvCib/1)。 –

+0

that works :)如果你能详细解释正则表达式,这将是非常好的吗?谢谢 –

+0

编辑:如果我删除“,”和“替换”之间的换行符,它仍然匹配替换字段中的所有反斜杠。我们可以以某种方式避免吗? –

回答

0

这部作品在PowerShell中($文本持有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 
}