Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
.net 正则表达式将字符串转换为另一个字符串powershell_.net_Regex_Powershell - Fatal编程技术网

.net 正则表达式将字符串转换为另一个字符串powershell

.net 正则表达式将字符串转换为另一个字符串powershell,.net,regex,powershell,.net,Regex,Powershell,我有一个字符串,如下所示 $mystring="t={p:1,q:2,r:3}" $list=[System.Text.RegularExpressions.Regex]::Matches($mystring,"[{:,]").Value foreach($dicitem in $diclist) { $dic=$dic.Replace("$dicitem","$dicitem'") } $list=[System.Text.RegularExpressions.Regex]::Matches

我有一个字符串,如下所示

$mystring="t={p:1,q:2,r:3}"
$list=[System.Text.RegularExpressions.Regex]::Matches($mystring,"[{:,]").Value
foreach($dicitem in $diclist)
{
$dic=$dic.Replace("$dicitem","$dicitem'")
}
$list=[System.Text.RegularExpressions.Regex]::Matches($dic,"[}:,]").Value
foreach($dicitem in $diclist)
{
$dic=$dic.Replace("$dicitem","'$dicitem")
}
我想将此字符串转换为以下字符串

"t={'p':'1','q':'2','r':'3'}"
我怎么能在powershell中做到这一点

下面给出了我尝试的代码

$mystring="t={p:1,q:2,r:3}"
$list=[System.Text.RegularExpressions.Regex]::Matches($mystring,"[{:,]").Value
foreach($dicitem in $diclist)
{
$dic=$dic.Replace("$dicitem","$dicitem'")
}
$list=[System.Text.RegularExpressions.Regex]::Matches($dic,"[}:,]").Value
foreach($dicitem in $diclist)
{
$dic=$dic.Replace("$dicitem","'$dicitem")
}

但是我没有得到预期的结果,有没有其他更好的方法来做到这一点呢。搜索正则表达式:

(?<!\})(?!.*\{)

执行正则表达式替换。搜索正则表达式:

(?<!\})(?!.*\{)
试着这样做:

$mystring -replace '(?<={.*)([^:,}])', ("'{0}'" -f '$1')
$mystring-replace'(?请尝试以下方法:

$mystring -replace '(?<={.*)([^:,}])', ("'{0}'" -f '$1')

$mystring-replace'(?
$mystring-replace'(?
$mystring-replace')(?替换运算符更简单。此解决方案使用反向引用而不是-f格式运算符

编辑:最初误读了问题(遗漏了需要引用的字母)。 更新解决方案:

$mystring="t={p:1,q:2,r:3}"
$mystring -replace '([^{,]+):([^,}])+',"'`$1':'`$2'"

t={'p':'1','q':'2','r':'3'}

-replace运算符更简单。此解决方案使用反向引用而不是-f格式运算符

编辑:最初误读了问题(遗漏了需要引用的字母)。 更新解决方案:

$mystring="t={p:1,q:2,r:3}"
$mystring -replace '([^{,]+):([^,}])+',"'`$1':'`$2'"

t={'p':'1','q':'2','r':'3'}