无法使Java分割功能与正则表达式一起使用

时间:2022-03-24 21:26:44

I have a datetime string of the form YYYYDDMMHHSS that I want to split into its constituent parts.

我有一个YYYYDDMMHHSS形式的日期时间字符串,我想将其拆分为其组成部分。

From my understanding of regular expressions (based on Perl and Javascript) I was under the impression that the following should work:-

根据我对正则表达式的理解(基于Perl和Javascript),我认为以下内容应该起作用: -

String dt[] = c.getString(c.getColumnIndex("datetime")).split("(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)");

where c is a single entry cursor. But it doesn't.

其中c是单个条目游标。但事实并非如此。

I've searched around SO and Google generally but, although there are examples of split they are just splitting on a single character.

我一般都在搜索SO和谷歌,但是,虽然有分裂的例子,但它们只是分裂在一个角色上。

How can I split out the date time into its constituent parts?

如何将日期时间拆分为其组成部分?

2 个解决方案

#1


0  

Try to parse it with:

尝试解析它:

Date date;    
String dtStart = "YYYYDDMMHHSS";
    SimpleDateFormat format = new SimpleDateFormat("yyyyddMMHHss");  //mm -> minutes??
    try {
        date = format.parse(dtStart);
        System.out.println("Date ->" + date);
    } catch (Exception e) {
        e.printStackTrace();
    }

#2


0  

You can match groups with this regex:

您可以使用此正则表达式匹配组:

public static void main(String[] args) {
    String string = "200426122313";;
    Pattern pattern = Pattern.compile("(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)");
    Matcher matcher = pattern.matcher(string);
    matcher.find();
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
    System.out.println(matcher.group(4));
    System.out.println(matcher.group(5));
}

then get output:

然后得到输出:

2004
26
12
23
13

etc. The parentheses in regex ara a capturing groups, which capture a fragments of text, you want to extract.

正则表达式中的括号是一个捕获组,它捕获要提取的文本片段。

You can also split string with:

你也可以拆分字符串:

(?<=^\d{4}|^\d{6}|^\d{8}|^\d{10})

like in:

public static void main(String[] args) {
    String string = "200413122323";
    System.out.println(Arrays.toString(string.split("(?<=^\\d{4}|^\\d{6}|^\\d{8}|^\\d{10})")));
}

to get:

[2004, 13, 12, 23, 23]

#1


0  

Try to parse it with:

尝试解析它:

Date date;    
String dtStart = "YYYYDDMMHHSS";
    SimpleDateFormat format = new SimpleDateFormat("yyyyddMMHHss");  //mm -> minutes??
    try {
        date = format.parse(dtStart);
        System.out.println("Date ->" + date);
    } catch (Exception e) {
        e.printStackTrace();
    }

#2


0  

You can match groups with this regex:

您可以使用此正则表达式匹配组:

public static void main(String[] args) {
    String string = "200426122313";;
    Pattern pattern = Pattern.compile("(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)");
    Matcher matcher = pattern.matcher(string);
    matcher.find();
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
    System.out.println(matcher.group(4));
    System.out.println(matcher.group(5));
}

then get output:

然后得到输出:

2004
26
12
23
13

etc. The parentheses in regex ara a capturing groups, which capture a fragments of text, you want to extract.

正则表达式中的括号是一个捕获组,它捕获要提取的文本片段。

You can also split string with:

你也可以拆分字符串:

(?<=^\d{4}|^\d{6}|^\d{8}|^\d{10})

like in:

public static void main(String[] args) {
    String string = "200413122323";
    System.out.println(Arrays.toString(string.split("(?<=^\\d{4}|^\\d{6}|^\\d{8}|^\\d{10})")));
}

to get:

[2004, 13, 12, 23, 23]