Solved

How do I extract a number from a string, then change the number and insert it back into the string using coldfusion?

Posted on 2008-06-18
20
176 Views
Last Modified: 2013-12-16
I retrieve a string from a webservice in the following format: '$2.00 to $3.00 USD'

I want to change it to '$1.00 to $1.75'.  Also, most of the time the string will be in a format like '$2.00 USD'.  How do I test for which format and make the changes?

I'm stumped! Thanks for the help in advance!
0
Comment
Question by:duanehardy
  • 13
  • 7
20 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 21815414
Check this:
<cfset theString = 'This is my string conatining the prize: $2.00 to $3.00 USD, and more info.' >
 
<cfset newStr = ReReplace(theString, "\$(\d+\.\d\d)[^\d]+\$(\d+\.\d\d)( USD)?",'$1.00 to $1.75') >
 
<cfoutput>#newStr#</cfoutput>

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21815435
Sorry, I see atypo.
Check this:
<cfset theString = 'This is my string conatining the prize: $2.00 to $3.00 USD, and more info.' >
 
<cfset newStr = ReReplace(theString, "\$(\d+\.\d\d)[^\$]+\$(\d+\.\d\d)( USD)?",'$1.00 to $1.75') >
 
<cfoutput>#newStr#</cfoutput>

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21815608
And if you want to transform the prizes, then like this:
<cfset theString = 'This is my string conatining the prize: $2.00 to $3.00 USD, and more info.' >
 
<cfset thePrize = ListToArray(ReReplace(theString, "^[\s\S]*\$(\d+\.\d\d)[^\$]+\$(\d+\.\d\d)[\s\S]*$",'\1,\2')) >
<cfset newStr = ReReplace(theString, "\$(\d+\.\d\d)[^\$]+\$(\d+\.\d\d)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' to $'&NumberFormat(thePrize[2]/24*14,'0.99')) >
 
<cfoutput>#newStr#</cfoutput>

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:duanehardy
ID: 21817171
What do you mean by transform?  Also what does the .99 do in the expression?  I do want to split in half the amount, so the second example looks on target.

One last item, most of the time it is just '$2.00 USD' which I want to cut in half.  Will the last comment you sent (thePrize, newStr) work whether ithe strint is '$2.00 USD' or '$2.00 to $3.00 USD'?

Thanks for the help.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21817412
Transform means calculation formula, like your "to cut in half"
So what is your formula for the calculation?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21817846
How about this:

 
<cfset theString = 'This is my string conatining the prize: $42.00 to $83.00 USD, and more info.' >
 
<cfset thePrize = ListToArray(ReReplace(theString, "[^\$]*?\$*?(\d+\.\d\d)[^\$]*",'\1,','all')) >
 
 
 
 
<cfoutput>
<cfif ArrayLen(thePrize) eq 1 >
<cfset newStr = ReReplace(theString, "\$(\d+\.\d\d)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' USD') >
<cfelseif ArrayLen(thePrize) eq 2 >
<cfset newStr = ReReplace(theString, "\$(\d+\.\d\d)[^\$]+\$(\d+\.\d\d)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' to $'&NumberFormat(thePrize[2]/2,'0.99')&' USD') >
</cfif>
</cfoutput>
 
 
<cfoutput>
#newStr#
</cfoutput>

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21829447
Any feedback?
0
 

Author Comment

by:duanehardy
ID: 21835954
Sorry, had to travel out of town on business and just got back home to try it out.  I am getting some errors like 'USD 5.0 cannot be converted to a number'.

Turns out not all the strings are consistent.  Most are the format I have given, but others include: 'USD 5.0', 'USD 2.0 - USD 3.0'.  Could this be throwing the error.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21842060
One more extention and you need the super trooper function doWhatImean(;-)


<cfset theString = 'This is my string conatining the prize: USD 42.00 to USD 83.00 and more info.' >
 
<cfset thePrize = ListToArray(ReReplace(theString, "[\s\S]*?(\$|USD|to)\s*(\d+\.\d\d)",'\2,','all')) >
 
 
 
<cfoutput>
<cfif ArrayLen(thePrize) eq 1 >
<cfset newStr = ReReplace(theString, "(\$|USD)\s*(\d+\.\d\d)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' USD') >
<cfelseif ArrayLen(thePrize) gt 1 >
<cfset newStr = ReReplace(theString, "(\$|USD)\s*(\d+\.\d\d)[\$USDto\s]+(\d+\.\d\d)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' to $'&NumberFormat(thePrize[2]/2,'0.99')&' USD') >
</cfif>
</cfoutput>
 
 
<cfoutput>
#newStr#
</cfoutput>

Open in new window

0
 

Author Comment

by:duanehardy
ID: 21842994
Zvonko,

I really appreciate all your help.  We are so close but it is still throwing errors with 1 character behind the decimal point 'USD 42.0' (see example: http://67.199.18.39/ArrayTest.cfm ) and just outputs the string and does not do the division if the separator is '-'; for instance 'USD 42.00 - USD 84.00' (see example: http://67.199.18.39/ArrayTest1.cfm )

Thanks again for all your help.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21843401
Add the dash char to the matchin character set.
Like this:
<cfset newStr = ReReplace(theString, "(\$|USD)\s*(\d+\.\d\d)[\$USDto\s\-]+(\d+\.\d\d)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' to $'&NumberFormat(thePrize[2]/2,'0.99')&' USD') >


Now tell me please what the response is regularly.
Post a list of let say five different complete response strings.
But please show the complete responses.
Because when you get ONLY the prizes then you can split the strings and search for digits only.
Show me that five complete response strings.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21843412
For one decimal digit is this the way:


<cfset theString = 'USD 42.0' >

<cfset thePrize = ListToArray(ReReplace(theString, "[\s\S]*?(\$|USD|to)\s*(\d+\.\d\d?)",'\2,','all')) >
 
 
 
<cfoutput>
<cfif ArrayLen(thePrize) eq 1 >
<cfset newStr = ReReplace(theString, "(\$|USD)\s*(\d+\.\d\d?)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' USD') >
<cfelseif ArrayLen(thePrize) gt 1 >
<cfset newStr = ReReplace(theString, "(\$|USD)\s*(\d+\.\d\d?)[\$USDto\s\-]+(\d+\.\d\d?)( USD)?",'$'&NumberFormat(thePrize[1]/2,'0.99')&' to $'&NumberFormat(thePrize[2]/2,'0.99')&' USD') >
</cfif>
</cfoutput>
 
 
<cfoutput>
#newStr#
</cfoutput>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21843433
And this one is when you look only for didgits and all digits are prizes:


<cfset theString = 'This is my string conatining the prize: USD 42.00 - USD 84.00  and more info.' >
 
 
<cfset thePrize = ListToArray(ReReplace(theString, "[^\d]*?(\d+\.\d\d?)",'\1,','all')) >
 
 
 
<cfoutput>
<cfif ArrayLen(thePrize) eq 1 >
<cfset newStr = ReReplace(theString, "\b(\d+\.\d\d?)\b",'$'&NumberFormat(thePrize[1]/2,'0.99')) >
<cfelseif ArrayLen(thePrize) gt 1 >
<cfset newStr = ReReplace(theString, "\b(\d+\.\d\d?)(\D+)(\d+\.\d\d?)",'$'&NumberFormat(thePrize[1]/2,'0.99')&' \2 $'&NumberFormat(thePrize[2]/2,'0.99')) >
</cfif>
</cfoutput>
 
 
<cfoutput>
#newStr#
</cfoutput>

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21843436
Sorry , the first RegExp should be like this:

<cfset thePrize = ListToArray(ReReplace(theString, "\D*?(\d+\.\d\d?)",'\1,','all')) >


0
 

Author Comment

by:duanehardy
ID: 21846139
Thank you so much.  This works great!
0
 

Author Comment

by:duanehardy
ID: 21846722
Wow Zvonko,

I just got thrown a curve.  I have one vendor out of a thousand that sends the string as a percentage.  Same format, either '13.0%' or '3.0% - 6%'.

Is there a way to look for both $'s and %'s ,perform the same split (thePrize/2), and output as either '$2.00', '$2.00 - $4.00', '13.00%' or '3% - 6%'?  We could drop the USD from the dollars.

Thanks again.
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 21846983
Check this:

<cfset theString = '3% - 6%' >
 
<cfset thePrize = ListToArray(ReReplace(theString, "[^\d]*?(\d+\.?\d{0,2})",'\1,','all')) >
 
 
 
<cfoutput>
<cfif ArrayLen(thePrize) eq 1 >
<cfset newStr = ReReplace(theString, "\b(\d+\.?\d{0,2})\b",'$'&NumberFormat(thePrize[1]/2,'0.99')) >
<cfelseif ArrayLen(thePrize) gt 1 >
<cfset newStr = ReReplace(theString, "\b(\d+\.?\d{0,2})(\D+)(\d+\.?\d{0,2})",NumberFormat(thePrize[1]/2,'0.99')&' \2 '&NumberFormat(thePrize[2]/2,'0.99')) >
</cfif>
</cfoutput>
 
 
<cfoutput>
#newStr#
</cfoutput>

Open in new window

0
 

Author Comment

by:duanehardy
ID: 21847837
Zvonko,

you are the man! It works for both %'s and $'s.  Did you get the 500 points that I sent?  I tried to split it among two of your answers (not done that before) and I wanted to make sure everything worked properly.

I did have one question on the $ format.  'USD 42.0' returns 'USD $21.00' but 'USD 42.0 - USD 84.0' returns 'USD 21.00 and USD 42.00' (no $ sign in fron of numbers) .  Can we get the $ sign added back, and what about dropping USD?

Thanks.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21848168
I did not get neither $500 nor 500% ;-)

It sounds difficult to impossible.
But please can we close this one and start a fresh approach please?
0
 

Author Comment

by:duanehardy
ID: 21849857
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question