|Welcome to the Monastery|
Help with regex - find captured pattern twiceby pachydermic (Beadle)
|on Apr 09, 2013 at 21:24 UTC||Need Help??|
pachydermic has asked for the wisdom of the Perl Monks concerning the following question:
Hey guys, my problem involves rewriting equations with regular expressions. I have a list of special "endogenous" variables which need to be changed from "x" to "xSS*exp(x)" everywhere they occur in an equation. I'm working with time series, so "x(-1)" or "x(+1)" needs to be changed to "xSS*exp(x(-1))" or "xSS*exp(x(+1))", as well.
For example, let's suppose that my endogenous variables are:y, c, k, a, and h.
I need to take this:y = a*(k(-1)ˆalpha)*(hˆ(1-alpha));
and turn it into this:(ySS*exp(y)) = (aSS*exp(a))*((kSS*exp(k(-1)))ˆalpha)*((hSS*exp(h))ˆ(1-alpha));
Now I've already gotten started on this. Assume I already have a list of variables and equations to work with (contained in a single string which also has new lines). My code is:
And that's mostly okay except for the equation where you find "c/c(+1)" because the "/" character is already captured when the code finds "c/" - therefore "/c(+1)" isn't replaced. You can't just ignore the first (\W) because then parameters like "theta" would be problematic.
I know my regexs aren't very good anyways, so I'd like some advice about how to handle this problem.Thank you very much for your time - hopefully I've explained everything clearly.