Solved

parsing a string containing A

Posted on 2011-03-01
10
289 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
 
LVL 28

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

16 Experts available now in Live!

Get 1:1 Help Now