开发者

sed - or another tool for regex text replacement

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜