• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing Files with spaces

 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20886
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20886
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic