Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

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

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
duanehardy
Asked:
duanehardy
  • 13
  • 7
1 Solution
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
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
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
duanehardyAuthor Commented:
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
 
ZvonkoSystems architectCommented:
Transform means calculation formula, like your "to cut in half"
So what is your formula for the calculation?
0
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
Any feedback?
0
 
duanehardyAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
duanehardyAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
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
 
ZvonkoSystems architectCommented:
Sorry , the first RegExp should be like this:

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


0
 
duanehardyAuthor Commented:
Thank you so much.  This works great!
0
 
duanehardyAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
duanehardyAuthor Commented:
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
 
ZvonkoSystems architectCommented:
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
 
duanehardyAuthor Commented:
0
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.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

  • 13
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now