Avatar of PierceWeb
PierceWebFlag for United States of America asked on

Coldfusion ReReplace or CFScript Regular Expression needed

Hello,

I'm using Coldfusion to capture the exchange rate between Euros and US Dollars, using this code:

<cfhttp url="http://www.google.com/ig/calculator?hl=en&q=1EUR=?USD" method="get" timeout="5">
<cfoutput><cfset euroRate = #HTMLCodeFormat(cfhttp.FileContent)#>
#euroRate#
</cfoutput>

Open in new window

The output looks similar to this:
{lhs: "1 Euro",rhs: "1.3337 U.S. dollars",error: "",icc: true}

Can you please assist me with Coldfusion code required to extract the 1.3337 value?

Thank you,

Tom
ColdFusion LanguageRegular Expressions

Avatar of undefined
Last Comment
PierceWeb

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Terry Woods

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
PierceWeb

Hi Terry,

I tried your recommendation, but likely need a more plug-n-play bit of code. I got errors returned.

The value returned from Google may also be wrapped in a hidden <PRE>. I ran the value through HTMLEditFormat(#euroRate#) and got this:

<PRE>{lhs: &quot;1 Euro&quot;,rhs: &quot;1.3337 U.S. dollars&quot;,error: &quot;&quot;,icc: true}</PRE>

That would take care of having to escape the quotes. Might take multiple replacements to clean that up.

Ultimately I'm hoping to end up with just the value of 1.3337 that I can save in a variable and use in a math equation.

Thanks,

Tom
Terry Woods

Can you post what you've tried? I'm not a coldfusion coder but I've seen some of the code before so might be able to help anyway.
ASKER
PierceWeb

So far, I have the following hack that I just came up with, but am looking for a more eloquent solution. Depending upon the number of decimals included in a retrieved value, this solution may have issues.

<cfhttp url="http://www.google.com/ig/calculator?hl=en&q=1EUR=?USD" method="get" timeout="5">
<cfset euroRate = #HTMLCodeFormat(cfhttp.FileContent)#>
<cfset euroRate = HTMLEditFormat(#euroRate#)>
<cfoutput>#euroRate#</cfoutput>
<cfset euroRate = #Left(euroRate, 65)#>
<cfset euroRate = #Right(euroRate, 10)#>
<p><cfset euroRate = rereplace(euroRate, "[^0-9\.]", "", "all")>
<cfoutput>#euroRate#</cfoutput> (Euros per U.S. Dollar)

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
_agx_

(Edit)

I can't help you w/regex's.. so I'll leave that to TerryAtOpus :)

But it looks like JSON.  So you might try deserializing it into a structure.  Then you can access the values by key name.  If the currency values are *always* :

     "(number)(space)(description ie "Euro/US Dollars")

you could use list functions, with a space delimiter, to grab the 1st element ie (number)

<cfsavecontent variable="content">
{lhs: "1 Euro",rhs: "1.3337 U.S. dollars",error: "",icc: true}
</cfsavecontent>

<cfset parsed = deserializeJSON(content)>
<cfset USAmount = listFirst(parsed.rhs, chr(32))>
<cfset EuroAmount = listFirst(parsed.lhs, chr(32))>

Dollars <cfdump var="#USAmount #">
Euros <cfdump var="#EuroAmount #">

BTW, when using CFHTTP  *always* check the status code before parsing the result.  If the request fails, the fileContent will contain an error message, not the "{...}" string you're expecting. So your code ill fail with some weird error, and you won't know why.
SOLUTION
_agx_

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
PierceWeb

@TerryAtOpus and @_agx_

It works! Awesome!

Terry your regular expression works great so I'm awarding you most of the points, but agx's plug-n-play code helped bring it to life, so I'm awarding him a portion of the points too.

Thanks guys! You ROCK!

Tom