Get the date, time, and integer value from a java sentence

advertisements

Assume that the sentence in english:

String s = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";

And the sentence in chinese:

String s = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

I have try

  String[] words = s.split("\\s+");
  for (int i = 0; i < words.length; i++) {
  System.out.println(words[i]);
  }

And I get for eng sentence

13:10:47,829 INFO  [STDOUT] Your
13:10:47,829 INFO  [STDOUT] Last
13:10:47,829 INFO  [STDOUT] Login
13:10:47,829 INFO  [STDOUT] was
13:10:47,829 INFO  [STDOUT] 2013/10/04
13:10:47,829 INFO  [STDOUT] 13:06:45
13:10:47,829 INFO  [STDOUT] (
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Days,
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Hours,
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Minutes
13:10:47,829 INFO  [STDOUT] )

And for the chinese sentence:

13:11:49,712 INFO  [STDOUT] 您上次登录是
13:11:49,712 INFO  [STDOUT] 2013/10/04
13:11:49,712 INFO  [STDOUT] 13:07:15(
13:11:49,712 INFO  [STDOUT] 0
13:11:49,712 INFO  [STDOUT] 天,
13:11:49,712 INFO  [STDOUT] 0
13:11:49,712 INFO  [STDOUT] 小时
13:11:49,712 INFO  [STDOUT] 4
13:11:49,712 INFO  [STDOUT] 分钟
13:11:49,712 INFO  [STDOUT] )

In the result I can easily get the date, time and integer value from the eng sentences. But when the sentence change to chinese it cannot get the value using same way. Because the number of array after the sentence split was different. Is there any way I can get date, time and integer value from a sentence even in different language and the number of array return after sentence split was different.


 public static void main(String[] args) {
    String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
    String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

    String datePattern = "\\d\\d\\d\\d/\\d\\d/\\d\\d";
    String timePattern = "\\d\\d:\\d\\d:\\d\\d";

    System.out.println(getMatch(english, datePattern));
    System.out.println(getMatch(english, timePattern));
    System.out.println(getMatch(english, "\\d Days"));
    System.out.println(getMatch(english, "\\d Hours"));
    System.out.println(getMatch(english, "\\d Minutes"));
    System.out.println();
    System.out.println(getMatch(chinese, datePattern));
    System.out.println(getMatch(chinese, timePattern));
    System.out.println(getMatch(chinese, "\\d 天"));
    System.out.println(getMatch(chinese, "\\d 小时"));
    System.out.println(getMatch(chinese, "\\d 分钟"));
}

private static String getMatch(String input, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    if (matcher.find()) {
        return matcher.group();
    } else {
        return "";
    }
}