0

我有一个类似于下面示例的数组,但要大得多(4000 行长):

array(
   "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => Needs Quotes Around Me
), "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => Needs Quotes Around Me
        "d" => Needs Quotes Around Me
)
);

出于某种原因,字符串值没有引号 ("") 和冒号分隔符。有些字符串是数字,但可以被视为字符串,有些有空格和@符号,因为有些是电子邮件地址,但我需要将它们全部包装起来"STRING HERE",

我试图将 reg_replace 与这样的东西一起使用,=>\s([a-zA-Z0-9\@\s])+$但它不会用它找到的字符串替换匹配的字符串?我已经做了很多谷歌搜索,但似乎无法正确,请告诉我我哪里出错了。

我最终得到的是:

array(
       "id" => array(
            "a" => "", 
            "b" => "", 
            "c" => "[a-zA-Z0-9\@\s]",
    ), "id" => array(
            "a" => "", 
            "b" => "", 
            "c" => "[a-zA-Z0-9\@\s]",
            "d" => "[a-zA-Z0-9\@\s]",
    )
);
4

2 回答 2

1

此 perl 脚本适用于给出的示例

perl -pe 's/(?<==> )(?!"|array\()(.*)/"$1",/' EXAMPLEFILE.txt

产生以下输出:

array(
   "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => "Needs Quotes Around Me",
), "id" => array(
        "a" => "", 
        "b" => "", 
        "c" => "Needs Quotes Around Me",
        "d" => "Needs Quotes Around Me",
)
);
于 2013-02-13T17:03:26.167 回答
0

它需要在正则表达式周围放置圆括号。听起来很简单,只有当您知道如何操作时才容易。

于 2013-02-13T10:50:44.583 回答