0

我正在寻找一种方法来替换除第一次出现的组或某些字符之外的所有内容。

例如以下随机字符串:

+Z1A124B555ND124AB+A555

1,5,2,4,A,B 和 + 在整个字符串中重复。

124、555 是同样重复出现的字符组。

现在,假设我想删除除了第一次出现的 555、A 和 B。

什么正则表达式是合适的?我可以想到一个替换所有的例子:

preg_replace('/555|A|B/','',$string);

像^那样的东西,但我想保留第一次出现......有什么想法吗?

4

3 回答 3

1

你的字符串总是用加号分隔吗?555、A 和 B 是否总是出现在第一个“组”中(由 + 分隔)?

如果是这样,您可以拆分、替换然后加入:

$input = '+Z1A124B555+A124AB+A555';
$array = explode('+', $input, 3); // max 3 elements
$array[2] = str_replace(array('555', 'A', 'B'), '', $array[2]);
$output = implode('+', $array);

附言。当我们可以使用简单的 str_replace 时,无需使用正则表达式


使用preg_replace_callback功能:

$replaced = array('555' => 0, 'A' => 0, 'B' => 0);
$input = '+Z1A124B555+A124AB+A555';
$output = preg_replace_callback('/555|[AB]/', function($matches) {
  static $replaced = 0;
  if($replaced++ == 0) return $matches[0];
  return '';
}, $input);
于 2012-08-02T20:35:43.570 回答
1

可以修改此解决方案以执行您想要的操作:PHP: preg_replace (x) 发生?

这是一个修改后的解决方案:

<?php
class Parser {

    private $i;

    public function parse($source) {
        $this->i=array();
        return preg_replace_callback('/555|A|B/', array($this, 'on_match'), $source);
    }

    private function on_match($m) {
        $first=$m[0];
        if(!isset($this->i[$first]))
        {
            echo "I'm HERE";
            $this->i[$first]=1;
        }
        else
        {

            $this->i[$first]++;
        }
        

        
        // Return what you want the replacement to be.
        if($this->i[$first]>1)
        {
            $result="";
        }
        else
        {
            $result=$m[0];
        }
        return $result;
    }
}

$sample = '+Z1A124B555ND124AB+A555';
$parse = new Parser();
$result = $parse->parse($sample);
echo "Result is: [$result]\n";
?>
于 2012-08-02T21:11:36.673 回答
0

一个更通用的函数,适用于每种模式。

function replaceAllButFirst($pattern, $replacement, $subject) {

  return preg_replace_callback($pattern,

    function($matches) use ($replacement, $subject) {
      static $s;
      $s++;
      return ($s <= 1) ? $matches[0] : $replacement;
    },

    $subject
  );
}
于 2016-01-15T23:43:12.503 回答