PierceWeb
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:
{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
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>
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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)
(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)(descripti on 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.
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)(descripti
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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#)
<PRE>{lhs: "1 Euro",rhs: "1.3337 U.S. dollars",error: "",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