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:

my $model = "c*theta*hˆ(1+psi)=(1-alpha)*y;\n1 = beta*(c/c(+1)*alpha*y +(+1)/k+(1-delta));\ny = a*(k(-1)ˆalpha)*(hˆ(1-alpha));\nk = y-c+(1-de +lta)*k(-1);\nln(a) = rho*ln(a(-1))+ e;\n"; my @varlist = ('y','c','k','a','h'); my ($replacement,$search_string); foreach my $var (@varlist){ $search_string = quotemeta($var); $replacement = "$steady_states{$var}*exp(${var})"; $model =~ s/(\W)$search_string(\W)/$1($replacement)$2/g; $model =~ s/^$search_string(\W)/($replacement)$1/g; $model =~ s/exp\($search_string(\))+(\([\+\-]1\))/exp($search_st +ring$2$1)/g; } print $model;

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.

In reply to Help with regex - find captured pattern twice by pachydermic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.