fork download
  1. <?php
  2.  
  3. $re = <<<'EOF'
  4. /(?J)
  5. (?&R)\K\s*(?<sign>[=+*-])\s*(?=(?&R))
  6. |
  7. (?&R)\s*\K\s*(?<sign>=)\s*(?=-(?&R))
  8.  
  9. (?(DEFINE)
  10. (?<R>(?<!\w) ( (?:f|r|s) (?:1[0-5]|[0-9]) | CI | 1)))/mix
  11. EOF;
  12.  
  13. $str = <<<'EOF'
  14. s3=f2+f5*f8+f9 # Should match all the = + - and *
  15. foo=f5+bar*f7+f8+f9 # Should only match the last two '+'
  16.  
  17. SUBSTITUTION
  18.  
  19. f5=f5 + f6
  20. f5=f5-f6
  21. f5=f5+f6+CI-1
  22. f4=-r4
  23. f8=(f3+f3)/2
  24. 456e-3+5
  25. EOF;
  26.  
  27. # echo $re."\n";
  28.  
  29. # echo $str."\n";
  30.  
  31. # echo "---"."\n";
  32.  
  33.  
  34. function my_replace($matches) {
  35. return ' ' . $matches['sign']. ' ';
  36. }
  37. echo $output = preg_replace_callback($re, 'my_replace', $str);
  38.  
  39. ?>
  40.  
  41.  
Success #stdin #stdout 0.03s 25896KB
stdin
Standard input is empty
stdout
s3 = f2 + f5 * f8 + f9      # Should match all the = + - and *
foo=f5+bar*f7 + f8 + f9 # Should only match the last two '+'

SUBSTITUTION

f5 = f5 + f6
f5 = f5 - f6
f5 = f5 + f6 + CI - 1
f4 = -r4
f8=(f3 + f3)/2
456e-3+5