nospam0123456us wrote:

> I think it is even not enough for a complete solution, as each
> sub-expression is calculated by itself ex "1 + 3/5" should give 1.6 
> your solution return 1.0 eval("1.0 + 3/5") 

Good point. I changed the regex to deal with that case, see below.
> 
> intermediate expression :
> "A + b / 5 + 7 * 6"

I'm only interested in purely numerical expressions.
Anyhow, the bigger issue isn't so much regex, but eval() which will reject 
letters as undeclared variable 
names

> You/I can also imagine some tricky expression like :
> "a + MyClass.elements[ 5/b ] + math.sin( 5 ) + ( 45 % 6 & 25 )"  

The new regex can replace all integer numbers in the above expression, but - 
again - eval fails on 
undeclared variables.

Here's the new regex packaged in a function that clips a (multiline) selection 
and appends the value of 
each line to the selection:

input:
1/3
1 + 3/5
(1+1)/(4-1)

output:
1/3=0.333333
1 + 3/5=1.6
(1+1)/(4-1)=0.666667

code:
global CRLF=esc(?"\r\n",?"\")
....@selcalcpaste("","=")

function SelCalcPaste(sClip,sSep)
if( ""==sClip )do
  clip.copy
  sClip == clip.get
endif

local res=""
for each line i in sClip
res++=if(""!=res,CRLF);;+
++ifelse(""!=sSep,i++sSep,"");;+
++ftos(eval(;;+
regex.pcrereplace(?"(.*?)(?<![.,\d])(\d+)\b(?![.,])(.*?)";;+
,i,?"$1$2.0$3","anchored dotall");;+
))
endfor

clip.setpaste(res)
wait.for(50)

quit(res)

Reply via email to