2017-05-08 97 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 
}