Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Specific String search Coldfusion

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
Errol Farro
Asked:
Errol Farro
  • 2
1 Solution
 
_agx_Commented:
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
 
ansudhindraCommented:
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
 
Errol FarroAuthor Commented:
Thanks a lot Ansudhindra. Works like a charm....
0
 
_agx_Commented:
> "[\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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now