Solved

parsing a string containing A

Posted on 2011-03-01
10
293 Views
Last Modified: 2013-12-24
I have refernce codes in an url and need to parse them.

I have them in a string with: B002-MA3-A02

the dash - separates the different codes. Right now I need to get the code begining with A
so the result should be A02

how would I do this?
0
Comment
Question by:Shawn
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 35013775
Depends on the pattern.  Do you always want the 3rd item, if yes:
   <cfset str = "B002-MA3-A02">
   <cfset code = getToken(str, 3, "-")>
   <cfoutput>#code#</cfoutput>

... or one beginning with A followed by two numbers?
0
 
LVL 1

Author Comment

by:Shawn
ID: 35013949
it won't always be the 3rd item. It will however always begin with an A and end with either the delimiter - or be the last item.
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35014833
1. Use FIND to find the occurrence of the string "-A", that would be the beginning of the code.

2. Then again use find to find occurrence of "-", that would be the end of the code.

3. Then use MID function to get the string you are looking for.

Code below

<cfset strToCheck = 'B002-MA3-A02' />

<cfset firstOccurance = Find("-A","#strToCheck#")>

<cfset secondOccurance = Find('-','#strToCheck#',firstOccurance)>

<cfset startFrom = firstOccurance + 1 />

<cfset count = firstOccurance + secondOccurance />

<cfset codeRequired = MID('#strToCheck#',startFrom,count) />

<cfdump var="#codeRequired#">

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 29

Expert Comment

by:Pravin Asar
ID: 35018047
You could create custom string processing function based on Java
this functions returns the array of strings split at the specified delimiter.

<cfscript>
function MySplitString(inputStr,delim) {
      sts=createObject("java","java.lang.String");
      sts.init(inputStr);
      tokens=sts.split(delim);
      return tokens;
}
WriteDump (MySplitString("B002-MA3-A02",'-'));
</cfscript>
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 35018288
pavinasar, coldfusion has list and array functions built in...  listToArray() would also do what you're suggesting

brij, your solution won't work if the first element is the one with the "A" as in  "A12-B00-C34" because there is no "-A" to find.  I also won't work if the A is in the last element as in "X12-Y45-A10" because there is no "-" to find after the A element.

I suggest, if you want the third element, then use agx's code from the first post...

 <cfset code = getToken("B002-MA3-A02", 3, "-")>

If you want to find an element, regardless of where it is, that starts with the letter "A" you can use a simple loop..

<cfset myString = "B002-MA3-A02">
<cfset theStringYouWant = "">

<cfloop index="aVal" list="#myString#" delimiters="-">
   <cfif left(aVal,1) is "A">
      <cfset theStringYouWant = aVal>
      <cfbreak>
    </cfif>
</cfloop>

<cfoutput>#theStringYouWant#</cfoutput>

0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35018539
Thanks for pointing

here is the code, taken care of the conditions mentioned above

<cfset strToCheck = 'C02-B00245-A2345' />

<cfparam name="flag" default="0" />
<cfif Mid('#strToCheck#',1,1) EQ 'A'>
	<cfset flag = 1 />
	<cfset firstOccurance = 1 >
<cfelse>
	<cfset firstOccurance = Find("-A","#strToCheck#") + 1>
</cfif>

<cfset secondOccurance = Find('-','#strToCheck#',firstOccurance)>

<cfif secondOccurance EQ 0>
	<cfset startFrom = firstOccurance />
    <cfset count = len('#strToCheck#') - #firstOccurance# + 1 />
    <cfset codeRequired = MID('#strToCheck#',startFrom,count) />
    <cfdump var="#codeRequired#">
    <cfabort>
<cfelse>
<cfif flag>
	<cfset startFrom = 1 />
    <cfset count = secondOccurance - 1 />
<cfelse>
	<cfset startFrom = firstOccurance />
    <cfset count = secondOccurance - firstOccurance />
</cfif>
	<cfset codeRequired = MID('#strToCheck#',startFrom,count) />
    <cfdump var="#codeRequired#">
</cfif>

Open in new window

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35018808

If you really really want to use the mid() function instead of the list functions, you can do this...

<cfset strToCheck = 'C02-B00245-A2345' />

<cfset firstOccurence  = FindNoCase("-A", "-" & strToCheck) + 1>
<cfset secondOccurence = Find('-',strToCheck & "-",firstOccurence)>

<cfset codeRequired = MID(strToCheck, firstOccurence, (secondOccurence - firstOccurence))>

<cfdump var="#codeRequired#">
0
 
LVL 11

Expert Comment

by:Brijesh Chauhan
ID: 35019020
Actually I use this code to parse HTML and get some data, say for example something like <td>1234 </td>, so basically use the logic to find <td> and starting from the find, get </td>, then use the mid function to get 1234.

So when I saw this question that is what first came to my mind as this is something that I have used quite a lot.

0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35019229
>  parse HTML and get some data, say for example something like <td>1234 </td>,

You can also try something like this.   Once you get to the closest cell, this will return the value within it..

<cfset TheString = "<td>123</td>"
<cfset theValue =  REReplace(TheString,"<[^>]+>","","all")>

0
 
LVL 1

Author Closing Comment

by:Shawn
ID: 35019233
exactly what I was looking for. the A can be found anywhere in the string.

thank you
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 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