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
LVL 2
PierceWebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
Using an ReReplace with the following pattern might work:
^.*?rhs:\s*"([\d.]+)\s.*$
With replacement:
$1

You'll need to escape the " character if the pattern is enclosed in them.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PierceWebAuthor Commented:
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
0
Terry WoodsIT GuruCommented:
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.
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

PierceWebAuthor Commented:
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

0
_agx_Commented:
(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.
0
_agx_Commented:
@PierceWeb

Also, give TerryAtOpus 's suggestion a try. I just tested it with Railo and it worked fine.

Code:
<cfset result = reReplace(cfhttp.fileContent, '^.*?rhs:\s*\"([\d.]+)\s.*$', "\1")>
<cfoutput>
result = #result#
</cfoutput>

Output:

result = 1.3337
0
PierceWebAuthor Commented:
@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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.