说明
我会先分割字符串攻击这个问题分成要么引用或不带引号的字符串组。
然后通过匹配进行迭代,如果填充了组1,则该字符串被引用,因此只需替换捕获组0即可进行简单替换。如果未填充捕获组1,则跳至下一个匹配项。
在每一次迭代中,你都会想要建立一个新的字符串。
由于分割字符串是困难的部分,我会用这个表达式:
("[^"]*")|[^"]*

例
示例文字
"mission podcast" modcast A B C "D E F"
个
代码
PHP Code Example:
<?php
$sourcestring="your source string";
preg_match_all('/("[^"]*")|[^"]*/i',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);
?>
捕捉组
$matches Array:
(
[0] => Array
(
[0] => "mission podcast"
[1] => modcast A B C
[2] => "D E F"
[3] =>
)
[1] => Array
(
[0] => "mission podcast"
[1] =>
[2] => "D E F"
[3] =>
)
)
PHP实例
这PHP脚本将只替换引号中的字符串内的空间。
工作例如:http://ideone.com/jBytL3
代码
<?php
$text ='"mission podcast" modcast A B C "D E F"';
preg_match_all('/("[^"]*")|[^"]*/',$text,$matches);
foreach($matches[0] as $entry){
echo preg_replace('/\s(?=.*?")/ims','~~new~~',$entry);
}
输出
"mission~~new~~podcast" modcast A B C "D~~new~~E~~new~~F"
那么你到目前为止尝试过什么? –
你能提供更多的例子,而不是'find/vol_stor/8s8a912hj1 | grep“”mission \ | podcast“”| grep“modcast”'?至少2株将是多大的帮助 – Angga
嗯,我还没有尝试任何事情,因为我不知道如何保护范围内匹配字符串的一部分,只有更换它们的括号内的字中有什么。所以,我愿意接受各种建议:) –