I defined a function in Mathematica to process a pre-generated string. More specifically, I'd like to process some parts of C codes generated from MMA. Basically what my function does is to expand the
pow function in C, so it involves finding a string pattern and replacing it. I know using MMA is not the best way to solve this problem, but unfortunately the language I'm most familiar with is MMA...Anyway, here's my function:
ExpandPowInCode[code_] := StringReplace[code, Shortest["pow(" ~~ x__ ~~ "," ~~ y__ ~~ ")"] -> If[StringMatchQ[x, ___ ~~ "+" ~~ __] || StringMatchQ[x, ___ ~~ "-" ~~ __], Nest[StringJoin["(" <> ToString[x] <> ")", "*" ~~ #] &, "(" <> ToString[x] <> ")", ToExpression[y, InputForm] - 1], Nest[StringJoin[x, "*" ~~ #] &, x, ToExpression[y, InputForm] - 1]]]
It works as I expected, namely expanding , for example,
If condition will help when dealing with plus/minus sign in the first argument of
pow. For example,
"(a x+b)*(a x+b)*(a x+b)"
However, at the first time I executed
ExpandPowInCode, I always got the following error messages:
StringMatchQ::strse: String or list of strings expected at position 1 in StringMatchQ[x,___~~+~~__]. >>
StringMatchQ::strse: String or list of strings expected at position 1 in StringMatchQ[x,___~~-~~__]. >>
ExpandPowInCode still gave me the correct result, the appearance of error messages was really annoying...Moreover, when I executed the function again later, the error messages no longer appeared as if everything were correct. Has anyone ever met this odd situation?