Skip to main content

Module java_regex

Module java_regex 

Source
Expand description

java.util.regex match and replacement semantics over the regex crate.

Kafka’s principal-mapping rules, sasl.kerberos.principal.to.local.rules and ssl.principal.mapping.rules, are Java regular expressions applied with Matcher.matches(), Matcher.replaceAll and Matcher.replaceFirst. The regex crate differs from Java in the three places those rules touch:

  • Whole-input match. Matcher.matches() succeeds when some match of the pattern spans the whole input, found by backtracking. A leftmost-first find that happens to stop short, as a|ab does on ab, is not the same test. JavaPattern::matches runs a separately compiled \A(?:pattern)\z.

  • Group references in the replacement. Java reads $ then one digit, and extends the number digit by digit only while the result is still a group of the pattern, so $1_$2 is group 1, _, group 2 and $12 with one group is group 1 then 2. The regex crate takes the longest [_0-9A-Za-z] run as the group name and expands an unknown group to nothing. A backslash escapes the next character, and malformed references are errors rather than empty text.

  • Empty matches during replaceAll. Java retries one character past an empty match and allows an empty match right after a non-empty one, so x* over xx with [$0] is [xx][].

  • Predefined classes. Without UNICODE_CHARACTER_CLASS, Java’s \d, \w, \s and \b are ASCII-only; the regex crate’s are Unicode. JavaPattern::new rewrites them to their ASCII forms. It also makes any escaped non-alphanumeric character a literal, as Java does, where the regex crate rejects an escaped , or @.

Otherwise the pattern dialect is the regex crate’s: constructs it does not support, such as back-references and look-around, fail to compile.

Structs§

JavaPattern
A pattern compiled for Java’s matches, replaceAll and replaceFirst.

Enums§

JavaReplacementError
Why a Java replacement string could not be expanded against a match.