简体   繁体   English

Java Split无法按预期工作

[英]Java Split not working as expected

I am trying to use a simple split to break up the following string: 00-00000 我试图使用一个简单的拆分来分解以下字符串:00-00000

My expression is: ^([0-9][0-9])(-)([0-9])([0-9])([0-9])([0-9])([0-9]) 我的表达式是: ^([0-9][0-9])(-)([0-9])([0-9])([0-9])([0-9])([0-9])

And my usage is: 我的用法是:

String s = "00-00000";

String pattern = "^([0-9][0-9])(-)([0-9])([0-9])([0-9])([0-9])([0-9])";

String[] parts = s.split(pattern);

If I play around with the Pattern and Matcher classes I can see that my pattern does match and the matcher tells me my groupCount is 7 which is correct. 如果我使用Pattern和Matcher类,我可以看到我的模式匹配,匹配器告诉我我的groupCount是7这是正确的。 But when I try and split them I have no luck. 但是,当我尝试分裂他们时,我没有运气。

String.split does not use capturing groups as its result. String.split不使用捕获组作为结果。 It finds whatever matches and uses that as the delimiter. 它找到任何匹配并将其用作分隔符。 So the resulting String[] are substrings in between what the regex matches. 因此,结果String []是正则表达式匹配之间的子串。 As it is the regex matches the whole string, and with the whole string as a delimiter there is nothing else left so it returns an empty array. 因为正则表达式匹配整个字符串,并且整个字符串作为分隔符,所以没有其他任何东西留下,所以它返回一个空数组。

If you want to use regex capturing groups you will have to use Matcher.group() , String.split() will not do. 如果你想使用正则表达式捕获组,你将不得不使用Matcher.group() ,String.split()将不会。

for your example, you could simply do this: 举个例子,你可以这样做:

String s = "00-00000";

String pattern = "-";

String[] parts = s.split(pattern);

I can not be sure, but I think what you are trying to do is to get each matched group into an array. 我不能确定,但​​我认为你要做的是将每个匹配的组放入一个数组中。

    Matcher matcher = Pattern.compile(pattern).matcher();

    if (matcher.matches()) {
        String s[] = new String[matcher.groupCount()) {
           for (int i=0;i<matches.groupCount();i++) {
               s[i] = matcher.group(i);
            }
         }
    }

From the documentation: 从文档:

String[] split(String regex) -- Returns: the array of strings computed by splitting this string around matches of the given regular expression String[] split(String regex) - 返回:通过围绕给定正则表达式的匹配拆分此字符串计算的字符串数组

Essentially the regular expression is used to define delimiters in the input string. 本质上,正则表达式用于定义输入字符串中的分隔符。 You can use capturing groups and backreferences in your pattern (eg for lookarounds), but ultimately what matters is what and where the pattern matches, because that defines what goes into the returned array. 您可以在模式中使用捕获组和反向引用(例如,用于外观),但最重要的是模式匹配的内容和位置,因为它定义了返回数组的内容。

If you want to split your original string into 7 parts using regular expression, then you can do something like this: 如果要使用正则表达式将原始字符串拆分为7个部分,则可以执行以下操作:

    String s = "12-3456";
    String[] parts = s.split("(?!^)");

    System.out.println(parts.length); // prints "7"

    for (String part : parts) {
        System.out.println("Part [" + part + "]");
    } // prints "[1] [2] [-] [3] [4] [5] [6] "

This splits on zero-length matching assertion (?!^) , which is anywhere except before the first character in the string. 这会分为零长度匹配断言(?!^) ,除了字符串中的第一个字符之外的任何地方。 This prevents the empty string to be the first element in the array, and trailing empty string is already discarded because we use the default limit parameter to split . 这可以防止空字符串成为数组中的第一个元素,并且尾随空字符串已被丢弃,因为我们使用默认limit参数进行split

Using regular expression to get individual character of a string like this is an overkill, though. 但是,使用正则表达式来获取像这样的字符串的单个字符是一种过度杀伤力。 If you have only a few characters, then the most concise option is to use foreach on the toCharArray() : 如果你只有几个字符,那么最简洁的选择是在toCharArray()上使用foreach

    for (char ch : "12-3456".toCharArray()) {
        System.out.print("[" + ch + "] ");
    }

This is not the most efficient option if you have a longer string. 如果您有一个更长的字符串,这不是最有效的选项。


Splitting on - 拆分-

This may also be what you're looking for: 这也可能是您正在寻找的:

    String s = "12-3456";
    String[] parts = s.split("-");

    System.out.println(parts.length); // prints "2"

    for (String part : parts) {
        System.out.print("[" + part + "] ");
    } // prints "[12] [3456] "

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

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