Solved

Specific String search Coldfusion

Posted on 2012-12-27
4
249 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

12 Experts available now in Live!

Get 1:1 Help Now