Class Pattern

java.lang.Object
dev.gmitch215.bytebox.regex.Pattern

public final class Pattern extends Object
A compiled pattern, standing in for java.util.regex.Pattern.

The compiler substitutes java.util.regex.Pattern for this one, so a project matching the ordinary way works unchanged. Underneath it is the platform's own regular expressions, which the isolate already has: the class library's pattern compiler and matcher measure 68 KB of WebAssembly after compression, and V8 has an engine already.

The pattern is rewritten rather than passed through, because the two syntaxes agree about most things and disagree about a few that look identical - Translate lists them. What has no exact equivalent is refused at compile time with a PatternSyntaxException naming it, so a pattern either means here what it means on a JVM or it does not compile. Refused: atomic groups, possessive quantifiers, flags written inside the pattern, character-class intersection and nesting, \G, \X, and every character property except the POSIX names.

One difference that is not refused, because refusing case folding would be absurd: CASE_INSENSITIVE on its own folds only ASCII on a JVM, and the platform's folding is aware of the whole of Unicode. A pattern that relies on A not matching a non-ASCII letter with a similar case mapping will behave differently. Adding UNICODE_CASE makes the two agree.

Since:
1.0.0
  • Field Details

    • CASE_INSENSITIVE

      public static final int CASE_INSENSITIVE
      Fold case when matching.
      See Also:
    • COMMENTS

      public static final int COMMENTS
      Treat whitespace and # comments in the pattern as nothing.
      See Also:
    • MULTILINE

      public static final int MULTILINE
      Let ^ and $ match at every line.
      See Also:
    • LITERAL

      public static final int LITERAL
      Treat the pattern as a literal.
      See Also:
    • DOTALL

      public static final int DOTALL
      Let . match a line terminator.
      See Also:
    • UNICODE_CASE

      public static final int UNICODE_CASE
      Fold case across the whole of Unicode, which the platform does anyway.
      See Also:
    • CANON_EQ

      public static final int CANON_EQ
      Normalise before matching, which is refused.
      See Also:
    • UNIX_LINES

      public static final int UNIX_LINES
      Treat only \n as a line terminator.
      See Also:
    • UNICODE_CHARACTER_CLASS

      public static final int UNICODE_CHARACTER_CLASS
      Make the predefined classes match across the whole of Unicode, which is refused.
      See Also:
  • Method Details

    • compile

      public static Pattern compile(String pattern)
      Compiles a pattern.
      Parameters:
      pattern - the pattern
      Returns:
      the compiled pattern
      Throws:
      PatternSyntaxException - when the pattern is not one, or uses something with no exact equivalent here
    • compile

      public static Pattern compile(String pattern, int flags)
      Compiles a pattern with flags.
      Parameters:
      pattern - the pattern
      flags - the flags, combined with |
      Returns:
      the compiled pattern
      Throws:
      PatternSyntaxException - when the pattern is not one, or uses something with no exact equivalent here
    • matches

      public static boolean matches(String pattern, CharSequence input)
      Whether a whole input matches a pattern, compiling it each time.
      Parameters:
      pattern - the pattern
      input - what to match
      Returns:
      whether it matches
    • quote

      public static String quote(String text)
      A pattern that matches a literal.
      Parameters:
      text - the literal
      Returns:
      a pattern matching exactly it
    • matcher

      public Matcher matcher(CharSequence input)
      A matcher over an input.
      Parameters:
      input - what to search
      Returns:
      the matcher
    • pattern

      public String pattern()
      Returns the pattern as written.
      Returns:
      the pattern as written
    • flags

      public int flags()
      Returns the flags it was compiled with.
      Returns:
      the flags it was compiled with
    • toString

      public String toString()
      Returns the pattern as written.
      Overrides:
      toString in class Object
      Returns:
      the pattern as written
    • split

      public String[] split(CharSequence input)
      Splits an input around every match.
      Parameters:
      input - what to split
      Returns:
      the pieces
    • split

      public String[] split(CharSequence input, int limit)
      Splits an input around every match.
      Parameters:
      input - what to split
      limit - how many pieces at most, zero for no limit with trailing empties dropped, negative for no limit with them kept
      Returns:
      the pieces
    • asPredicate

      public Predicate<String> asPredicate()
      Returns a predicate answering whether the pattern is found anywhere in a string.
      Returns:
      a predicate answering whether the pattern is found anywhere in a string
    • asMatchPredicate

      public Predicate<String> asMatchPredicate()
      Returns a predicate answering whether the pattern matches a whole string.
      Returns:
      a predicate answering whether the pattern matches a whole string
    • splitAsStream

      public Stream<String> splitAsStream(CharSequence input)
      The pieces an input splits into, as a stream.
      Parameters:
      input - what to split
      Returns:
      the pieces
    • namedGroups

      public Map<String,Integer> namedGroups()
      Returns every named group, in the order the pattern declares them.
      Returns:
      every named group, in the order the pattern declares them