|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
extracting numbers from a string
I am parsing a paypal history(csv) to update records in a databse.Matthew Smith 09/06/08 10:51 A If you're looking to get the number at the beginning of the string:Andy Matthews 09/06/08 02:55 P ><cfset primayKey = REMatchNoCase('^#[0-9]+',str)>Matthew Smith 09/06/08 04:14 P I am parsing a paypal history(csv) to update records in a databse. Here is an example of one of the values from one line of the csv: "#47-Pepper - 10 votes" The first number is the primary key from the db. I can parse it, but I am limited to 4 digits long with my solution, and it is not very elegant. Here's what I have: <cfset primarykey = mid(detail, 3, 1)> <cfif isnumeric(mid(detail, 4, 1))> <cfset primarykey = primarykey & mid(detail, 4, 1)> </cfif> <cfif isnumeric(mid(detail, 5, 1))> <cfset primarykey = primarykey & mid(detail, 5, 1)> </cfif> <cfif isnumeric(mid(detail, 6, 1))> <cfset primarykey = primarykey & mid(detail, 6, 1)> </cfif> Is there a better way to do what I am trying to do? Thanks. If you're looking to get the number at the beginning of the string: <cfset primayKey = REMatchNoCase('^#[0-9]+',str)> That will return an array with, using your current string, a single index. If you're wanting to get ALL numbers from the string: <cfset primayKey = REMatchNoCase('[0-9]+',str)> That returns the same array with, using your current string, two indexes. I am parsing a paypal history(csv) to update records in a databse. Here is an example of one of the values from one line of the csv: "#47-Pepper - 10 votes" The first number is the primary key from the db. I can parse it, but I am limited to 4 digits long with my solution, and it is not very elegant. Here's what I have: <cfset primarykey = mid(detail, 3, 1)> <cfif isnumeric(mid(detail, 4, 1))> <cfset primarykey = primarykey & mid(detail, 4, 1)> </cfif> <cfif isnumeric(mid(detail, 5, 1))> <cfset primarykey = primarykey & mid(detail, 5, 1)> </cfif> <cfif isnumeric(mid(detail, 6, 1))> <cfset primarykey = primarykey & mid(detail, 6, 1)> </cfif> Is there a better way to do what I am trying to do? Thanks. ><cfset primayKey = REMatchNoCase('^#[0-9]+',str)> I get an error when running that. I tried: <cfset primayKey = REMatchNoCase('^##[0-9]+',str)> but that just results in an empty array.
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||