• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

string split with regular expression

 
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Could you please help.
There is now 2 hours that I'm trying to fix this issue with regex ...

Text file composed of following



The goal is to save this information in a 2 dimensional array of double
I'm stuck with the regex.
It is not working, and I don't know how to approach the solution



What is wrong ?

Many thanks in advance

Christian
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is used for .delimiter
line.split("(-?\\d+(,\\d+)?)\t(-?\\d+(,\\d+)?)");

If you want to split, you may write only one symbol inside parameters. Try this.
line.split(" ");
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worse :-(



-20 1,43
java.lang.ArrayIndexOutOfBoundsException: 1
at pacp.test.WinRelP2T.openPressionTemperatureFile(WinRelP2T.java:106)
at pacp.test.WinRelP2T.<init>(WinRelP2T.java:60)
at pacp.test.WinRelP2T$1.run(WinRelP2T.java:46)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)


 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Christian Klugesherz wrote:Worse :-(



This is because you are not using a space as your delimiter. You are using a tab.

BTW, your first regular expression was pretty complex... including the use of capture groups.  So, obviously, you know your regular expressions. Why not use the regex engine (ie. the java.util.regex.Pattern and java.util.regex.Matcher classes), instead of the split() method of the String class?

Henry
 
bob john
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


There is very simple and clear example how to split. I used your text file.
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:This is because you are not using a space as your delimiter



Sorry, so stupid is my question  :-(
Working well with TAB :-)
 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all :-)

Henry Wong wrote:Why not use the regex engine


Hummm why not :-)

This gives the possibility to separate the 2 values in the line, whatever space or tab for the separation..



Almost work...
However still something wrong with the regular expression.
The whole line is stored in matcher m.


 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got for  now as print result

 
Christian Klugesherz
Ranch Hand
Posts: 111
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works with this code..
But not really understood why



 
Sheriff
Posts: 15784
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jamie Zawinski, in a Tue 12 Aug 1997 Usenet post wrote: Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.



There doesn't seem to be too much variation in your data. I would try to find a simpler way to parse that out. You should never go with code that you don't fully understand, especially when it "seems to work."
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic