donglun1918 2014-07-21 02:11
浏览 48
已采纳

替换文字星(*)...但仅在括号内

this my next question for regex. Honestly, i want to build animation for math multiply , when their input some formula, their will get animation to describe where is this number will counting, this part of my code.

$pattern = '([*])';
$replace = '||';
$stringAsli = (123*1.23*(0.12/8))*(123*512+40);
$replaceResult= str_replace($pattern,$replace,$stringAsli);

but i got result :

(123||1.23||(0.12/8))||(123||512+40)

i want result like this

(123||1.23||(0.12/8))*(123||512+40);

Extended Requirements

In this special case with quoted strings:

("123*1.23*n"*"(0.12/8)")*("123*512"+40)

should become

("123||1.23||n"$"(0.12/8)")*("123||512"+40)

In other words, still replace the * inside the brackets, but if we're inside a quoted strings we replace with ||, and if not we replace with $

  • 写回答

3条回答 默认 最新

  • dongzj2015 2014-07-21 02:15
    关注

    Recursive Regex and Lambda Replacement

    To safely get into your parentheses, we need to use a recursive regex. Fortunately, PHP supports this. Do it like this (see the output of the online demo):

    $mystring = "(123*1.23*(0.12/8))*(123*512+40)";
    $nested_parentheses_regex="~\((?:[^()]++|(?R))*\)~";
    $replaced = preg_replace_callback(
                        $nested_parentheses_regex,
                        function($m) { return str_replace("*","||",$m[0]);},
                        $mystring
                        );
    echo $replaced;
    

    See the output of the online demo.

    Explanation

    • The recursive regex \((?:[^()]++|(?R))*\) matches (potentially) nested parentheses
    • The preg_replace_callback function calls an anonymous function which replaces every * with a || within the match (which is a set of nested parentheses.

    Extended Requirements

    You now asked that inside the parentheses, when the asterisk is not inside quotes, we return $

    Use this:

    $mystring = '("123*1.23*n"*"(0.12/8)")*("123*512"+40)';
    $nested_parentheses_regex ="~\((?:[^()]++|(?R))*\)~"; 
    
    $replaced = preg_replace_callback($nested_parentheses_regex,
        function($m) {
              return preg_replace_callback(
                   '~"[^"]*"|(\*)~', // complete "quote" OR capture * to Group 1
                   function($n) {
                       // Not inside quotes?
                       if(isset($n[1])) return "\$";
                       else {  // we matched a "full quote"  
                          return str_replace("*","||",$n[0]);
                       }
                       }, 
                    $m[0] // this is a (full (set) of parentheses)
                    ); // end inner callback
        },
        $mystring
    ); // end outer callback
    echo $replaced;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大