Coldfusion ReReplace or CFScript Regular Expression needed


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

<cfhttp url="" method="get" timeout="5">
<cfoutput><cfset euroRate = #HTMLCodeFormat(cfhttp.FileContent)#>

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,

Who is Participating?
Terry WoodsConnect With a Mentor IT GuruCommented:
Using an ReReplace with the following pattern might work:
With replacement:

You'll need to escape the " character if the pattern is enclosed in them.
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.


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.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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="" method="get" timeout="5">
<cfset euroRate = #HTMLCodeFormat(cfhttp.FileContent)#>
<cfset euroRate = HTMLEditFormat(#euroRate#)>
<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


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}

<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.
_agx_Connect With a Mentor Commented:

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

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


result = 1.3337
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!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.