简体   繁体   中英

regex to split string in java

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.

Why not use a SimpleDateFormat for parsing the date part, which was designed for this task?

eg

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM