0

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.

2
  • 1
    So what pieces are you expecting? Commented Nov 10, 2010 at 19:45
  • I am expecting 2010-11-10 00:00:00,999, & some string follows Commented Nov 10, 2010 at 20:00

4 Answers 4

4

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!)

Sign up to request clarification or add additional context in comments.

10 Comments

is there someway to get the delimiter also? <sorry prev question was for some other answer>
What do you mean by "delimiter" here?
I tried using Pattern earlier could not get it working..the above does not seem to work either.
Sorry, I'm not anywhere I can test right now. What doesn't work about what I posted, and what didn't work in what you tried? Split() is definitely not the right approach, though
Pattern is the correct approach. Let's try and get that working for you
|
3

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];

3 Comments

That is true but I have to extract this part from the input string
yeah but I dont want to assume the date will be at the beginning.
@user444300: that's something that's useful to put into your question then. Don't leave requirements out of the question. From your description it seems very clear that the date will be at the beginning.
0

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.

Comments

-2

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.

1 Comment

The problem is not the regexp. It's the use of split

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.