开发者

sed - or another tool for regex text replacement

开发者 https://www.devze.com 2023-03-19 16:48 出处:网络
I need to replace all $arr[key] with $arr[\'key\'] but not $arr[$key] or $arr[CONST] or already rewritten as $arr[\'key\']

I need to replace all $arr[key] with $arr['key'] but not $arr[$key] or $arr[CONST] or already rewritten as $arr['key']

开发者_如何学Go

Eventually $arr[key] with {$arr['key']}

Thanks!


You could try something like:

perl -pe 's/(\$\w+\[\s*)(?![A-Z\d]+\b)(\w+)(\s*])/$1\'$2\'$3/g' file.php

Example:

echo $arr[foo] $arr['ok'] $arr[3] $arr[CAPS] $arr[$var] $arr[ bar ] | perl -pe "s/(\$\w+\[\s*)(?![A-Z\d]+\b)(\w+)(\s*])/$1'$2'$3/g"
$arr['foo'] $arr['ok'] $arr[3] $arr[CAPS] $arr[$var] $arr[ 'bar' ]
0

精彩评论

暂无评论...
验证码 换一张
取 消