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
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of PierceWeb
PierceWeb
Flag of United States of America image

ASKER

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
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

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.
Avatar of PierceWeb
PierceWeb
Flag of United States of America image

ASKER

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

Avatar of _agx_
_agx_
Flag of United States of America image

(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
Avatar of _agx_
_agx_
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of PierceWeb
PierceWeb
Flag of United States of America image

ASKER

@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
ColdFusion Language
ColdFusion Language

ColdFusion is a server-side rapid application development platform originally created by Allaire and now sold by Adobe, implementing the dynamic general purpose CFML programming language. The term ColdFusion is sometimes colloquially used to refer to the CFML language (Cold Fusion Markup Language), but can also include discussions of the server software implementation. ColdFusion runs using a customised version of Apache Tomcat. Earlier versions are bundled with JRun.

11K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo