| 1 | RE2/J: linear time regular expression matching in Java |
| 2 | ====================================================== |
| 3 | |
| 4 | [](https://github.com/google/re2j/actions/workflows/gradle.yaml?query=branch%3Amaster) |
| 5 | [](https://codecov.io/gh/google/re2j) |
| 6 | |
| 7 | RE2 is a regular expression engine that runs in time linear in the size of the |
| 8 | input. RE2/J is a port of C++ library [RE2](https://github.com/google/re2) to pure Java. |
| 9 | |
| 10 | Java's standard regular expression package, `java.util.regex`, and many other |
| 11 | widely used regular expression packages such as PCRE, Perl and Python use a |
| 12 | backtracking implementation strategy: when a pattern presents two alternatives |
| 13 | such as `a|b`, the engine will try to match subpattern `a` first, and if that |
| 14 | yields no match, it will reset the input stream and try to match `b` instead. |
| 15 | |
| 16 | If such choices are deeply nested, this strategy requires an exponential number |
| 17 | of passes over the input data before it can detect whether the input matches. |
| 18 | If the input is large, it is easy to construct a pattern whose running time |
| 19 | would exceed the lifetime of the universe. This creates a security risk when |
| 20 | accepting regular expression patterns from untrusted sources, such as users of |
| 21 | a web application. |
| 22 | |
| 23 | In contrast, the RE2 algorithm explores all matches simultaneously in a single |
| 24 | pass over the input data by using a _nondeterministic_ finite automaton. |
| 25 | |
| 26 | There are certain features of PCRE or Perl regular expressions that cannot be |
| 27 | implemented in linear time, for example, backreferences, but the vast majority |
| 28 | of regular expressions patterns in practice avoid such features. |
| 29 | |
| 30 | # Why should I switch? |
| 31 | |
| 32 | If you use regular expression patterns with a high degree of alternation, your |
| 33 | code may run faster with RE2/J. In the worst case, the `java.util.regex` |
| 34 | matcher may run forever, or exceed the available stack space and fail; this |
| 35 | will never happen with RE2/J. |
| 36 | |
| 37 | # Caveats |
| 38 | |
| 39 | This is not an official Google product (experimental or otherwise), it is just |
| 40 | code that happens to be owned by Google. |
| 41 | |
| 42 | RE2/J is not a drop-in replacement for `java.util.regex`. Aside from the |
| 43 | different package name, it doesn't support the following parts of the |
| 44 | interface: |
| 45 | |
| 46 | * the MatchResult class |
| 47 | * Matcher.hasAnchoringBounds() |
| 48 | * Matcher.hasTransparentBounds() |
| 49 | * Matcher.hitEnd() |
| 50 | * Matcher.region(int, int) |
| 51 | * Matcher.regionEnd() |
| 52 | * Matcher.regionStart() |
| 53 | * Matcher.requireEnd() |
| 54 | * Matcher.toMatchResult() |
| 55 | * Matcher.useAnchoringBounds(boolean) |
| 56 | * Matcher.usePattern(Pattern) |
| 57 | * Matcher.useTransparentBounds(boolean) |
| 58 | * CANON_EQ |
| 59 | * COMMENTS |
| 60 | * LITERAL |
| 61 | * UNICODE_CASE |
| 62 | * UNICODE_CHARACTER_CLASS |
| 63 | * UNIX_LINES |
| 64 | * PatternSyntaxException.getMessage() |
| 65 | |
| 66 | It also doesn't have parity with the full set of Java's character classes and |
| 67 | special regular expression constructs. |
| 68 | |
| 69 | # Getting RE2/J |
| 70 | |
| 71 | If you're using Maven, you can use the following snippet in your `pom.xml` to get RE2/J: |
| 72 | |
| 73 | ```xml |
| 74 | <dependency> |
| 75 | <groupId>com.google.re2j</groupId> |
| 76 | <artifactId>re2j</artifactId> |
| 77 | <version>1.6</version> |
| 78 | </dependency> |
| 79 | ``` |
| 80 | |
| 81 | You can use the same artifact details in any build system compatible with the Maven Central repositories (e.g. Gradle, Ivy). |
| 82 | |
| 83 | You can also download RE2/J the old-fashioned way: go to [the RE2/J release tag](https://github.com/google/re2j/releases), download the RE2/J JAR and add it to your CLASSPATH. |
| 84 | |
| 85 | # Discussion and contribution |
| 86 | |
| 87 | We have set up a Google Group for discussion, please join the [RE2/J discussion |
| 88 | list](http://groups.google.com/group/re2j-discuss) if you'd like to get in |
| 89 | touch. |
| 90 | |
| 91 | If you would like to contribute patches, please see the [instructions for |
| 92 | contributors](CONTRIBUTING.md). |
| 93 | |
| 94 | # Who wrote this? |
| 95 | |
| 96 | RE2 was designed and implemented in C++ by Russ Cox. The C++ implementation |
| 97 | includes both NFA and DFA engines and numerous optimisations. Russ also ported |
| 98 | a simplified version of the NFA to Go. Alan Donovan ported the NFA-based Go |
| 99 | implementation to Java. Afroz Mohiuddin wrapped the engine in a familiar Java |
| 100 | `Matcher` / `Pattern` API. James Ring prepared the open-source release |
| 101 | and has been its primary maintainer since then. |