Solved

Specific String search Coldfusion

Posted on 2012-12-27
4
252 Views
Last Modified: 2012-12-27
Hi all,

Can someone help me create the following CF Function?

We receive payments from clients but the client number can be anywhere in the description.
The naming convention of the client number is: yyyynnn where yyyy is the year and nnn is a sequence number between 001 and 999. The year can be between 1960 and the current year. There may be other numbers in the description but the client number should be returned only when the above criteria is met.

Thanks in advance.

Errol
0
Comment
Question by:Errol Farro
  • 2
4 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 38723672
You'll likely need a regex. You should add that topic to the question tags to attract attention from the right experts. (I'm not a regex guru so I'll leave this to someone more qualified :)
0
 
LVL 13

Accepted Solution

by:
ansudhindra earned 375 total points
ID: 38723707
check this..
<cfoutput>
  <cfset sourceString="This is a demo string and customer number is not 4537825 but it is 1998893 and not 19989990. Did you got it?" />
  Customer number from function is  #getCustomerNumber(sourceString)#.
</cfoutput>

<cffunction name="getCustomerNumber" returntype="numeric">
  <cfargument name="sourceString" required="true" type="string" />
  
  <cfset startYear = 1960 />
  <cfset endYear = dateformat(now(),'yyyy') />
    
  <cfset arrSearch = rematch("[\d]+",sourceString)>
  <cfset customerNumber = 0 />
  
  <cfloop from="1" to="#arraylen(arrSearch)#" index="i" step="1">
    <cfset cv = arrSearch[i] />
    <cfif len(trim(cv)) eq 7>
     <cfif ((left(cv,4) gte startYear) and (left(cv,4) lte endYear)) and ((right(cv,3) gt 0) and (right(cv,3) lt 1000))>
        <cfset customerNumber = cv />
        <cfreturn customerNumber />
     </cfif>
    </cfif>
  </cfloop> 
  <cfreturn customerNumber />
</cffunction>

Open in new window

0
 

Author Closing Comment

by:Errol Farro
ID: 38723787
Thanks a lot Ansudhindra. Works like a charm....
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38723970
> "[\d]+"

May as well search for 7 digits exactly instead of *any* number of digits.

And if you go with a cffunction, don't forget to VAR scope all those variables... or you can end up creating weird threading bugs in some cases.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
coldfusion, jQuery help 25 101
ColdFusion 10 Error 2 59
Scrolling feed with Coldfusion 3 49
How to get javascript to talk to coldfusion in < 3 page loads 4 51
This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 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