【问题标题】:How clear duplicate consecutive non-alphabetic characters in a string?如何清除字符串中重复的连续非字母字符?
【发布时间】:2017-10-29 23:49:57
【问题描述】:

仅匹配字符串:,.:- 如何从字符串中删除重复值?例如:

"ab::::c ---------d,,,e ..........f ::a-b,,,c..d"

预期输出:

"ab:c -d,e .f :a-b,c.d" 

【问题讨论】:

    标签: php regex duplicates preg-replace non-alphanumeric


    【解决方案1】:

    这里我们使用preg_replace 来实现所需的输出。

    正则表达式: ([,.:-])\1+ Regex demo

    或者

    正则表达式: (,|\.|:|-)\1+Regex demo

    1.这将匹配一个字符并将其添加到捕获的组中

    2. 使用捕获的组\1 不止一次出现。

    替换:$1

    Try this code snippet here

    <?php
    ini_set('display_errors', 1);
    
    $string="ab::::c ---------d,,,e ..........f ::a-b,,,c..d";
    echo preg_replace('/([,.:-])\1+/', '$1', $string);
    

    解决方案 2: using foreach loop

    Try this code snippet here

    $string="aab::::css ---------ddd,,,esddsff ..........f ::a-b,,,c..d";
    $chars=  str_split($string);
    $result=array();
    foreach($chars as $character)
    {
        if($character!=end($result) ||  !in_array($character, array(":",",",".","-")))
        {
            $result[]=$character;
        }
    }
    print_r(implode("",$result));
    

    【讨论】:

      【解决方案2】:

      您可以使用preg_replace

      preg_replace — 执​​行正则表达式搜索和替换

      $pattern = '/(\.|\,|\:|\-){2,}/';
      $string = 'ab::::c ---------d,,,e ..........f ::a-b,,,c..d';
      echo preg_replace($pattern, '$1', $string);
      

      您可以在这里尝试您的正则表达式:https://regex101.com/

      【讨论】:

        【解决方案3】:

        对于未来的读者,为了获得最大效率,请不要在您的模式中使用管道字符。使用循环的方法也进行了太多的迭代函数调用和/或条件。

        输入:$in="ab::::c ---------d,,,e ..........f ::a-b,,,c..d";

        方法#1:单行使用preg_replace()(注意空替换字符串)

        echo preg_replace('/([,.:-])\K\1+/','',$in);
        //                          ^^ resets the start of the matched substring
        

        方法 #2:使用preg_split() & implode() 的单行代码

        echo implode(preg_split('/([,.:-])\K\1+/',$in));  // empty glue doesn't need mentioning
        

        使用任一方法输出:

        ab:c -d,e .f :a-b,c.d
        

        我想知道在这个页面上哪种方法最有效。如果有人愿意使用 Sahil 的 2 种方法和我的两种方法运行并发布基准测试,那将非常有启发性。


        这是一个迟来的考虑...如果您的字符串在移动到有效字符之前仅存在符号重复自身的问题,那么您可以使用此模式:[-.,:]\K[-.,:]+ 它的执行速度将比所有其他模式快 50%此页面,它提供与此页面上的其他方法相同的输出,但确实延伸了您对问题的解释。以下是一些揭示差异的示例:

        ab:-,.c;将减少为ab:c
        ab:-,.c -d.,.e--f 将减少为ab:c -d.e-f

        这可能适合也可能不适合您的项目。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-09-02
          • 1970-01-01
          • 2021-09-10
          • 1970-01-01
          • 1970-01-01
          • 2015-01-26
          • 1970-01-01
          相关资源
          最近更新 更多