Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing and Reformatting

 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
I'm parsing the below text. But first, I want to remove the letters "CFP" and replace it with a line feed ("\n"). The line feed character would be placed right after the "KGS," for example. I tried to pass a String object of the text into a StringBuffer and use the StringBuffer's replace method, but that didn't seem to work. I'm hoping that someone could offer some suggestions.

But here's another problem. The text that's being parsed is more than just the above block. I'm trying to break the text into blocks like the one above so it's easier to handle. But for some reason this text has two of the same blocks.


As you can see, the only difference between the two blocks is the "CFP" and the "SHP." I want to grab the first one and ignore the second while removing the "CFP." Although it's optioinal I guess to remove the "CFP." Any suggestions would be appreciated.
[ April 07, 2003: Message edited by: Chris Cairns ]
[ April 07, 2003: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe something like

[ April 07, 2003: Message edited by: Jim Yingst ]
 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me restate the problem. I found that the text is formatted very sloppy and I need to reformat it. I'm going to paste the whole thing so you can understand my problem.

What I need to do is cut off the text from 0 to 69 as a substring. So if you take a look at the top of the text, you'll see "1CFP." That text including anything to the right needs cut off. I tried spiltting the string and grabbing a substring from positions 0 to 69 for each line. But I got OutOfBounds exception. Not sure what to try next.
 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the Exception is occurring because of Row 0. When I used String[] records = text.split("\n") to break into lines -- from which I'd grab substrings -- I got a print out that row 0 is null.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This regex should do what you want:

[ April 07, 2003: Message edited by: Jason Menard ]
 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regex is the bomb. I need to learn it fully!
 
mangesh lele
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
though i havent fully understood the problem, i have carried similar sorta text parsing using StringTokenizer, which i f . wonder if it will work out here...
 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't use anything from JDK 1.4. It has to be compatible with 1.3. Could I use StringTokenizer to solve my problem of cutting off the text?
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could download a third party regex package such as ORO or Regexp, both part of the Jakarta project. Or maybe you could use Perl, which excels at text processing.
 
Chris Cairns
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason,
As part of my earlier solution, I used the method split() in the String class. Unfortunately, that's part of 1.4. Anyway I could do the same sort of thing you're talking about with regex?

Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic