0

在我的文本编辑器(phpStorm、notepad++、jedit 等)中,我有如下字符串:

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

我需要将它们替换为:

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

IE

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

但是:我需要额外的内联替换“-”->“_”,转换为小写并在每个大写字母前加上“_”。

这可能使用 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 回答