我有一个类似下面例子的数组,但要大得多(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]",
)
);
答案 0 :(得分: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",
)
);
答案 1 :(得分:0)
需要在正则表达式周围放置圆形括号。这听起来很简单,只有当你知道如何时才会这么容易。