Solved

Specific String search Coldfusion

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Coldfusion graceful timeout 3 57
multi page pdf 4 88
code conversion assistance required 1 32
Using coldfusion <cfexecute> to restore mysql database 7 27
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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