Reputation: 9986
I have a text file in which I have to take line by line.
for each line I have to cut my string and put it in a table.
My separator is space.
Example of String:
22/03/2013 00h00 9940 10200 10260 10190 10030 10060
Example of result:
[22/03/2013 00h00, 9940, 10200, 10260, 10190, 10030, 10060]
my problem is that in my file, my separator varies from one line to another
Example:
22/03/2013 00h00 9940 10200 10260 10190 10030 10060
22/03/2013 01h00 9970 9900 9970 9850 9830 9740
22/03/2013 02h00 9630 9750 10010 10100 10040 10010
How I could cut my string ?
Upvotes: 0
Views: 107
Reputation: 784898
Something like should work for you:
String str = "22/03/2013 00h00 9940 10200 10260 10190 10030 10060 " +
"22/03/2013 01h00 9970 9900 9970 9850 9830 9740 " +
"22/03/2013 02h00 9630 9750 10010 10100 10040 10010";
// split string into individual records
String[] rows = str.split("\\s+(?=\\d{2}/\\d{2}/\\d{4} )");
// now split each row into columns (combine first 2 columns into 1)
for (String r: rows)
System.out.println(Arrays.toString(r.split("(?<!\\d{2}/\\d{2}/\\d{4})\\s+")));
OUTPUT:
[22/03/2013 00h00, 9940, 10200, 10260, 10190, 10030, 10060]
[22/03/2013 01h00, 9970, 9900, 9970, 9850, 9830, 9740]
[22/03/2013 02h00, 9630, 9750, 10010, 10100, 10040, 10010]
Explanation: First regex to split by 1 or more space/newline characters if it is followed by a date in nn/nn/nnnn format \\d{2}/\\d{2}/\\d{4}
.
Second regex is to split columns by 1 or more space/newline characters if it is NOT preceded by a date in the same format thus ignoring very first space after date.
Upvotes: 0
Reputation: 272217
Don't forget that String.split()
takes a regular expression. You can split on multiple whitespaces thus:
myString.split("\\s+");
This uses the character class \s
, representing any whitespace character (space, tab etc.)
It's not clear how well your data rows are defined in terms of spaces. Your best bet may be to split on whitespace as above, then take the first 2 elements and handle them as a date/time. e.g.
String[] results = myString.split("\\s+");
String datetime = results[0] + " " + results[1];
(checking for those result elements existing, etc.)
Upvotes: 2