This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Parsing Files with spaces

 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic