• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spliting a string on a second space and not all spaces

 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a series of stings that I want to get values for but there are extra spaces that cause extra gyrations I am hoping someone would be able to help me around.

The string has embedded newlines and returns which I split on first to get to the specific lines and now need to get the number on the line and associate it with a variable.

I tried the following but it drops a number off.



Giving me:
Fred is
00
Willma

Barney is
99

Any thoughts?

Mike
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number is part of the splitting regex and will therefore not show up in the non-matched parts. Take a look at the Javadoc of Pattern for how (positive) lookahead works.
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

Thanks that was the guide point I needed.

This worked:


Mike
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me like the line terminator pattern doesn't quite cut it either.
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle,


It seemed to work for me, did I miss something as to why does not quite work?

Mike
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the hard-coded String it'll work like a charm, unfortunately it won't successfully match line terminators for text files created on Windows, Unix (based) and older Mac OS operating systems. See http://en.wikipedia.org/wiki/Newline.
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks. I was not worried about OSs. (though I did not say that upfront.) And I did not catch that as an issue or even think about it, which I need to remember.

I am pulling data from a serial device. The coded strings are surrounded by newlines and returns.

Thanks
Mike
 
Ulrika Tingle
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Bates wrote:Any thoughts?


Sometimes it's easier and faster to do some old-fasioned parsing.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic