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 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角