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']
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' ]
精彩评论