| Author |
Parsing and Reformatting
|
Chris Cairns
Ranch Hand
Joined: Jan 31, 2003
Posts: 48
|
|
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
Joined: Jan 30, 2000
Posts: 18652
|
|
Maybe something like [ April 07, 2003: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Chris Cairns
Ranch Hand
Joined: Jan 31, 2003
Posts: 48
|
|
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
Joined: Jan 31, 2003
Posts: 48
|
|
|
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
Joined: Nov 09, 2000
Posts: 6450
|
|
This regex should do what you want: [ April 07, 2003: Message edited by: Jason Menard ]
|
Jason's Blog
|
 |
Chris Cairns
Ranch Hand
Joined: Jan 31, 2003
Posts: 48
|
|
|
Regex is the bomb. I need to learn it fully!
|
 |
mangesh lele
Greenhorn
Joined: Jan 17, 2003
Posts: 18
|
|
|
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
Joined: Jan 31, 2003
Posts: 48
|
|
|
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
Joined: Nov 09, 2000
Posts: 6450
|
|
|
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
Joined: Jan 31, 2003
Posts: 48
|
|
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!
|
 |
 |
|
|
subject: Parsing and Reformatting
|
|
|