Reputation: 13
I am planning to split a string in java using regex. My input will be as follows
2010-11-10 00:00:00,999, some string follows
I am using
inputString.split("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3},)");
This gives a string[] with two elements, second element is "some string follows" but first element is empty string, how to get the first part of the string.
Update: I cant assume the date will be at the beginning. Sorry for leaving it out earlier.
Upvotes: 0
Views: 2339
Reputation: 41777
You probably don't want split. Split's argument is the delimited between the strings. So you are succesfully matching the timestamp, but split is returning you the string before that (empty) and after ("some string follows").
You probably want
Matcher m = Pattern.compile("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3},)")
.matcher("2010-11-10 00:00:00,999, some string follows");
if (m.find()) {
String first_part = m.group(1);
}
(untested!)
Upvotes: 4
Reputation: 81174
Why not use a SimpleDateFormat for parsing the date part, which was designed for this task?
e.g.
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
Date date = fmt.parse("2010-11-10 00:00:00,999");
To get this part of the input string you could find the second occurrence of a comma and return everything before it:
String input = "2010-11-10 00:00:00,999, yada yada";
String dateInput = input.substring(0, input.indexOf(',', input.indexOf(',') + 1));
Or the regex way (using a positive lookbehind assertion to make sure it's the second comma):
String dateInput = input.split("(?<=.*,.*),")[0];
Upvotes: 3
Reputation: 4901
I don't think you are capturing the correct pieces. The regex (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3},)
captures the ENTIRE date. If you add capturing groups around each piece, then you can loop through each captured group. As to the two groups, the way Java regexes work is that group 0 is the entire matched string, then the groups thereafter are the captured groups.
Upvotes: -2
Reputation: 41498
Why not search for the second comma and break the string into two manually? Seems too complex to me to perform such a simple task. Of course, that would work assuming the format is always exactly the same.
Upvotes: 0