Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
180 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 2000 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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
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…

824 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