0

私のテキスト エディター (phpStorm、notepad++、jedit など) には、次のような文字列があります。

....    $this->request_json['store-user-id'] .....
....    $this->request_json['deviceID'] ....

それらを次のように置き換える必要があります。

$this->request->store_user_id
$this->request->device_id

すなわち

search: \-\>request_json\[\"([\w_\-]+)\"\]
replace: ->request->$1

BUT:追加のインライン置換 "-" -> "_"、小文字への変換、すべての大文字の前に "_" が必要です。

perlスタイルの正規表現を使用してそれは可能ですか? もしかして再帰?

4

2 に答える 2

0

$txt 文字列にこの 4 つの連続する正規表現置換を適用するだけです

$txt =~ s/_json\[\'/->/;
$txt =~ s/']//;
$txt =~ s/([a-z])([A-Z])/\1_\2/g;
$txt =~ tr/[A-Z]/[a-z]/;
于 2015-01-15T09:14:42.113 に答える
0

最後にphpの問題を解決しました:

$fstr = implode("", file("file_with_text_to_replace.php"));
$rfstr = preg_replace_callback("/\\-\\>request_json\\[(?:\\\"|\\')([\\w_\\-]+)(?:\\\"|\\')\\]/",
             function ($matches)
             {
               //any post-processing
               return  "->request->" . str_replace("-","_", $matches[1]);
             },
             $fstr);

最も強力なソリューションです。私は最近 php に少し触れていませんが、誰もこの php 関数を指摘していないことに非常に驚いています。テキスト エディターでは不可能な検索結果を完全に制御できます。素晴らしい!

于 2015-01-15T10:50:40.480 に答える