aspose file tools*
The moose likes Java in General and the fly likes Parsing Files with spaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing Files with spaces" Watch "Parsing Files with spaces" New topic
Author

Parsing Files with spaces

chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Hi,

I have a file in the following format. Each Columns are separated by one or multiple spaces

Column A Column B Column C Column D

Text1 2 3 Text4

Text2 & Text 3 4 5 Text5 and Text6

what i have to do is to extract column C or any column .

I have the following regular expression which splits the file :

String[] data = line.split("\\s+");

However, The data in each columns can also contain spaces. This regex splits that also. How do i go about splitting columns, also preserving the spaces in each column.

Is there a way to do it or its not possible


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

Is there a way to do it or its not possible


There is no black magic here -- you need to be able to do it yourself, in order to teach the computer how to do it. Having the criteria that a space may or may not separate a column is not good enough for a person to figure out what is a column. How do you expect your program to magically do it?

Anyway, is there an additional criteria? Something else that you can use to determine what is a column? After all, you seem to feel that it can be done.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3971
    
  17

How do you determine what spaces are supposed to be used to split columns and which aren't? If you can define that then there is a chance you can write a regex or some other logic to split it. If you can't then you are out of luck.

To me, given the imports you provided, I couldn't tell where one column ends and the next starts.


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

Steve Luke wrote:
To me, given the imports you provided, I couldn't tell where one column ends and the next starts.


I think that this is caused by the fact that the OP didn't format the post. Taking the orginal text as formatted, from the first post...




[EDIT: Okay, even the code tags doesn't format it correctly... sorry]

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing Files with spaces
 
Similar Threads
Read charcters from first line of a file
Dump row data into a string text
2d arrays
Transpose matrix
Set multiple columns using prepare statement