Skip to content

Behaviour of Matcher#split doesn't match java.util.regex #197

Description

@cushon

In the following example, the behaviour of re2j doesn't match the behaviour of java.util.regex, is this expected?

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;

public class T {
  public static void main(String[] args) {
    String pattern = "\n(?: *\n)+";
    String input = "";
    int limit = 0;
    {
      System.err.println("> java.util.regex");
      var p = java.util.regex.Pattern.compile(pattern);
      print(p.split(input, limit));
    }
    {
      System.err.println("\n> re2j");
      var p = com.google.re2j.Pattern.compile(pattern);
      print(p.split(input, limit));
    }
  }

  private static void print(String[] split) {
    System.err.println(
        stream(split).map(x -> String.format("'%s'", x)).collect(joining(", ", "[", "]")));
  }
}
> java.util.regex
['']

> re2j
[]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions