Reputation: 221
I've been trying to use the String.split()
method in the java programming language to retrieve data from a string.
The data in my string is in a weird format. Let me give you an example: let's say I have the following string:
String s="My name is : John Smith 13 75,5";
I need to split the string in such a way that everything (except whitespaces) before :
goes to a string, then, in what's left, everything (except whitespaces) before the first number to another string, then the first number to another string and the last number to another string, such that, considering the String s
in my example I would have the following output:
My name is
John Smith
13
75,5
I tried the following code:
data= s.split("\\s*\\:\\s*|\\s+|\\s+");
But the output was:
My
name
is
John
Smith
13
75,5
I've tried many other regular expressions but with no success (I believe that proves how much of a beginner I am with regex...) Can somebody help me?
Note: I think it wouldn't be too difficult to write my own split method for my data and maybe it's much better in terms of performance but I would really like to understand how to do this using regex.
Upvotes: 1
Views: 291
Reputation: 11867
"\\s*\\:\\s*|\\s+|\\s+"
The |
characters don't do what you think they might. They act as "or", so in your case your regex is matching \\s+
to the spaces between words, because while they don't match the first section of your regex, they certainly match the second (and third).
Better solution would be something like this:
"\\s*\\:\\s*|\\s{2,}"
Upvotes: 1
Reputation: 726699
If you want to keep single spaces, but split on 2+ spaces, the expression looks like this:
data= s.split("\\s*\\:\\s*|\\s{2,}");
This produces the output that you need (demo):
My name is
John Smith
13
75,5
Upvotes: 1