$re = '/([\w]+)[ \t]+=[ \t](Solver[ \t]*\(([\d.]+)[ \t]*,[ \t]*([\d.]+)[ \t]*,[ \t]*([\d.]+)[ \t]*\))\n\r?demo[ \t]*\(\1\.b[ \t]*,[ \t]*\1\.a[ \t]*,[ \t]*\1\.c[ \t]*\)/m';
$str = 'import math
class Solver(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def demo(b, a, c):
d = b ** 2 - 4 * a * c
if d >= 0:
disc = math.sqrt(d)
root1 = (- b + disc) / (2 * a)
root2 = (- b - disc) / (2 * a)
print(root1, root2)
return root1, root2
else:
raise Exception
s = Solver(2, 123, 0.025)
demo(s.b, s.a, s.c)';
$subst = "$2\.demo()";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php