"be consistent" PerlMonks

Re: Help with regex - find captured pattern twice

by hbm (Hermit)
 on Apr 09, 2013 at 23:37 UTC ( #1027865=note: print w/replies, xml ) Need Help??

A bit broader than you specified - this replaces any single letter and optional parenthetical value.

```\$_= "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-delta)*k(-
+1);\nln(a) = rho*ln(a(-1))+ e;\n";

s/(?<!\pL)(\pL)(\([\d+-]+\))?(?!\pL)/(\${1}SS*exp(\$1\$2))/g;

print;

#prints
(cSS*exp(c))*theta*(hSS*exp(h))^(1+psi)=(1-alpha)*(ySS*exp(y));
1 = beta*((cSS*exp(c))/(cSS*exp(c(+1)))*alpha*(ySS*exp(y(+1)))/(kSS*ex
+p(k))+(1-delta));
(ySS*exp(y)) = (aSS*exp(a))*((kSS*exp(k(-1)))^alpha)*((hSS*exp(h))^(1-
+alpha));
(kSS*exp(k)) = (ySS*exp(y))-(cSS*exp(c))+(1-delta)*(kSS*exp(k(-1)));
ln((aSS*exp(a))) = rho*ln((aSS*exp(a(-1))))+ (eSS*exp(e));

Replies are listed 'Best First'.
Re^2: Help with regex - find captured pattern twice
by pachydermic (Beadle) on Apr 11, 2013 at 13:52 UTC

Wow. What the heck is that monstrosity?! Well done.

Would you mind explaining that regex? I think that \pL references a unicode character class (from this page: http://perldoc.perl.org/perlrecharclass.html) but I'm still kind of baffled by what's going on...

You might read it as this:

```s/
(?<!        # 2. NOT preceded by
\pL      #    a letter
)

(           # 1. Capture
\pL      #    a letter
)

(           # 3. Capture
\(       #    open paren;
[\d+-]+  #    one or more digits, plus, minus;
\)       #    close paren
)?          # (Zero or one time)

(?!         # 4. NOT followed by
\pL      #    a letter
)

/(\${1}SS*exp(\$1\$2))/gx;

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1027865]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-09-18 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?