Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

parsing a string containing A

Posted on 2011-03-01
10
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 
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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
What You Need to Know when Searching for a Webhost Provider
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

610 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