• Post Reply Bookmark Topic Watch Topic
  • New Topic

string split with regular expression  RSS feed

 
Christian Klugesherz
Ranch Hand
Posts: 103
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
 
bob john
Ranch Hand
Posts: 116
  • 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: 103
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)

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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: 116
  • 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: 103
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: 103
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: 103
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got for  now as print result

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



 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!