Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Semi-Formatted Column

Status
Not open for further replies.

SgtJarrow

Programmer
Apr 12, 2002
2,937
0
0
US
I am having trouble with parsing a string of data. The data is semi-free flow - meaning there is no defined order, but the items in the column are (mostly) formatted. This is a trailer text column in a database I am trying to write a parser for to remove most of this ambiguity.

Given the following sample data:
Code:
TMI Y4.1/L7.2/R35 6598
TMI Y3.2/L7/R45
2450/D080808
TMI/Y4.1/L3/R3.5 5687
TMI/Y3.33/L5/R2.3

I would like to get:
Code:
Y4.1/L7.2/R35
Y3.2/L7/R45
D080808
Y4.1/L3/R3.5
Y3.33/L5/R2.3

The identifiers TMI and 2450 are dynamic in length - currently I only have identifiers that are three or four characters, but I can't expect that to always be true going forward. When an identifier is present, I need to get everything from that identifier to the next space in the trailer - except the space that might immediately follow the identifier.

I am close with the below code, but it has a problem when the first character after the tag is a space...The are sometimes other values in the trailers (more than one of these identifiers and a couple other possible values) but I already have the loop coded necessary to handle these instances. I am only having the problem on making this string extraction work properly.

Thanks for looking and helping me with my problem.

Code:
try
{
    trailerPart = remainingTrailer.Substring(remainingTrailer.IndexOf(ttag) + ttag.Length + 1, remainingTrailer.IndexOf(" ", remainingTrailer.IndexOf(ttag) + 1) - ttag.Length);
}
catch (ArgumentOutOfRangeException)  //If the tag is the last in the trailer
{
    trailerPart = remainingTrailer.Substring(remainingTrailer.IndexOf(ttag) + ttag.Length);
}

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Nevermind...I found it. Of course, speaking it out loud, even in a forum, helped me see the problem. I was missing ttag.length in my parsing line...

Code:
try
{
    trailerPart = remainingTrailer.Substring(remainingTrailer.IndexOf(ttag) + ttag.Length + 1, remainingTrailer.IndexOf(" ", remainingTrailer.IndexOf(ttag) + ttag.Length + 1) - ttag.Length);
}
catch (ArgumentOutOfRangeException)  //If the tag is the last in the trailer
{
    trailerPart = remainingTrailer.Substring(remainingTrailer.IndexOf(ttag) + ttag.Length);
}

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top