Can you use zero-width matching regex in String split?

后端 未结 3 1065
后悔当初
后悔当初 2020-11-30 14:17
System.out.println(
    Arrays.deepToString(
        \"abcghi\".split(\"(?:<)|(?:>)\")
    )
);

This prints [abc, def, ghi]<

相关标签:
3条回答
  • 2020-11-30 15:00

    You need to take a look at zero width matching constructs:

    (?=X)   X, via zero-width positive lookahead
    (?!X)   X, via zero-width negative lookahead
    (?<=X)  X, via zero-width positive lookbehind
    (?<!X)  X, via zero-width negative lookbehind
    
    0 讨论(0)
  • 2020-11-30 15:00

    Thanks to information from Cine, I think these are the answers I'm looking for:

    System.out.println(
        Arrays.deepToString(
            "abc<def>ghi<x><x>".split("(?=<)|(?<=>)")
        )
    ); // [abc, <def>, ghi, <x>, <x>]
    
    
    System.out.println(
        Arrays.deepToString(
            "Hello! Oh my!! Good bye!! IT WORKS!!!".split("(?<=!++)")
        )
    ); // [Hello!,  Oh my!!,  Good bye!!,  IT WORKS!!!]
    

    Now, the second one was honestly discovered by experimenting with all the different quantifiers. Neither greedy nor reluctant work, but possessive does.

    I'm still not sure why.

    0 讨论(0)
  • 2020-11-30 15:19

    You can use \b (word boundary) as what to look for as it is zero-width and use that as the anchor for looking for < and >.

    String s = "abc<def>ghi";
    String[] bits = s.split("(?<=>)\\b|\\b(?=<)");
    for (String bit : bits) {
      System.out.println(bit);
    }
    

    Output:

    abc
    <def>
    ghi
    

    Now that isn't a general solution. You will probably need to write a custom split method for that.

    Your second example suggests it's not really split() you're after but a regex matching loop. For example:

    String s = "Hello! Oh my!! Good bye!!";
    Pattern p = Pattern.compile("(.*?!+)\\s*");
    Matcher m = p.matcher(s);
    while (m.find()) {
      System.out.println("[" + m.group(1) + "]");
    }
    

    Output:

    [Hello!]
    [Oh my!!]
    [Good bye!!]
    
    0 讨论(0)
提交回复
热议问题