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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now