简体   繁体   English

使用正则表达式将字符串拆分为 3 部分

[英]Split a string using regex into 3 parts

I have a string like this我有一个这样的字符串

DATA/2019-00-01-23.x

I want to get three tokens Text, Date and Hour我想获得三个标记文本、日期和时间

[DATA, 2019-00-01, 23]

I tried this我试过这个

String x = "DATA/2019-00-01-23.x";
System.out.println(Arrays.toString(x.split("/|-[0-9]+.")))

This returns me这还给我

[DATA, 2019, 01, x]

You may actually use a split like您实际上可能会使用像这样的split

x.split("/|-(?=[^-]*$)|\\D+$")

See the Java demo , output: [DATA, 2019-00-01, 23] .参见Java 演示,输出: [DATA, 2019-00-01, 23]

This regex will split at这个正则表达式将在

  • / - a slash / - 斜线
  • | - or - 或者
  • -(?=[^-]*$) - last hyphen in the string -(?=[^-]*$) - 字符串中的最后一个连字符
  • | - or - 或者
  • \\D+$ - any 1+ non-digit chars at the end of the string (as String.split(regex) is run with limit argument as 0 , these matches at the end of the string do not result in trailing empty items in the resulting array.) \\D+$ - 字符串末尾的任何 1+ 非数字字符(因为String.split(regex)limit参数为0运行,字符串末尾的这些匹配不会导致尾随空项结果数组。)

Solution 1解决方案1

You can replace the last part after the dot, then using split with /|(\\-)(?!.*\\-) :您可以替换点之后的最后一部分,然后使用 split 和/|(\\-)(?!.*\\-)

String[] split = "DATA/2019-00-01-23.x".replaceFirst("\\..*$", "")
    .split("/|(\\-)(?!.*\\-)"); // [DATA, 2019-00-01, 23]

Solution 2解决方案2

I would go with Pattern and Matcher and groups like so (.*?)/(.*?)-([^-]+)\\\\..* :我会使用PatternMatcher以及这样的组(.*?)/(.*?)-([^-]+)\\\\..*

Pattern pattern = Pattern.compile("(.*?)/(.*?)-([^-]+)\\..*");
Matcher matcher = pattern.matcher("DATA/2019-00-01-23.x");
if(matcher.find()){
    System.out.println(matcher.group(1)); // DATA
    System.out.println(matcher.group(2)); // 2019-00-01
    System.out.println(matcher.group(3)); // 23
}

Or by using Java9+ you can use :或者通过使用 Java9+,您可以使用:

String[] result = Pattern.compile("(.*?)/(.*?)-([^-]+)\\..*")
        .matcher("DATA/2019-00-01-23.x")
        .results()
        .flatMap(grps -> Stream.of(grps.group(1), grps.group(2), grps.group(3)))
        .toArray(String[]::new);

Outputs输出

[DATA, 2019-00-01, 23]

Use capturing groups to extract the three parts.使用捕获组来提取三个部分。

private static final Pattern PATTERN = Pattern.compile("(.+)/([-0-9]+)-([0-9]{1,2})\\..*");

public static void main(String... args) {
    Matcher matcher = PATTERN.matcher("DATA/2019-00-01-23.x");

    if (matcher.matches() && matcher.groupCount() == 3) {
        String text = matcher.group(1);
        String date = matcher.group(2);
        String hour = matcher.group(3);
        System.out.println(text + "\t" + date + '\t' + hour);
    }
}

Dissected: (.+) / ([-0-9]+) - ([0-9]{2}) \\..*解剖: (.+) / ([-0-9]+) - ([0-9]{2}) \\..*

  • (.+) Everything before the / (.+) /之前的所有内容
  • ([-0-9]+) Numbers, can contain - ([-0-9]+)数字,可以包含-
  • - to prevent the previous part from gobbling up the hour -防止前一部分吞噬时间
  • ([0-9]{2}) Two numbers ([0-9]{2})两个数字
  • \\..* A period, then 'the rest'. \\..*一个句号,然后是“其余的”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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