Class Pattern
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intNormalise before matching, which is refused.static final intFold case when matching.static final intTreat whitespace and#comments in the pattern as nothing.static final intLet.match a line terminator.static final intTreat the pattern as a literal.static final intLet^and$match at every line.static final intFold case across the whole of Unicode, which the platform does anyway.static final intMake the predefined classes match across the whole of Unicode, which is refused.static final intTreat only\nas a line terminator. -
Method Summary
Modifier and TypeMethodDescriptionReturns a predicate answering whether the pattern matches a whole string.Returns a predicate answering whether the pattern is found anywhere in a string.static PatternCompiles a pattern.static PatternCompiles a pattern with flags.intflags()Returns the flags it was compiled with.matcher(CharSequence input) A matcher over an input.static booleanmatches(String pattern, CharSequence input) Whether a whole input matches a pattern, compiling it each time.Returns every named group, in the order the pattern declares them.pattern()Returns the pattern as written.static StringA pattern that matches a literal.String[]split(CharSequence input) Splits an input around every match.String[]split(CharSequence input, int limit) Splits an input around every match.splitAsStream(CharSequence input) The pieces an input splits into, as a stream.toString()Returns the pattern as written.
-
Field Details
-
CASE_INSENSITIVE
public static final int CASE_INSENSITIVEFold case when matching.- See Also:
-
COMMENTS
public static final int COMMENTSTreat whitespace and#comments in the pattern as nothing.- See Also:
-
MULTILINE
public static final int MULTILINELet^and$match at every line.- See Also:
-
LITERAL
public static final int LITERALTreat the pattern as a literal.- See Also:
-
DOTALL
public static final int DOTALLLet.match a line terminator.- See Also:
-
UNICODE_CASE
public static final int UNICODE_CASEFold case across the whole of Unicode, which the platform does anyway.- See Also:
-
CANON_EQ
public static final int CANON_EQNormalise before matching, which is refused.- See Also:
-
UNIX_LINES
public static final int UNIX_LINESTreat only\nas a line terminator.- See Also:
-
UNICODE_CHARACTER_CLASS
public static final int UNICODE_CHARACTER_CLASSMake the predefined classes match across the whole of Unicode, which is refused.- See Also:
-
-
Method Details
-
compile
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
Compiles a pattern with flags.- Parameters:
pattern- the patternflags- 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
Whether a whole input matches a pattern, compiling it each time.- Parameters:
pattern- the patterninput- what to match- Returns:
- whether it matches
-
quote
A pattern that matches a literal.- Parameters:
text- the literal- Returns:
- a pattern matching exactly it
-
matcher
A matcher over an input.- Parameters:
input- what to search- Returns:
- the matcher
-
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
Returns the pattern as written. -
split
Splits an input around every match.- Parameters:
input- what to split- Returns:
- the pieces
-
split
Splits an input around every match.- Parameters:
input- what to splitlimit- how many pieces at most, zero for no limit with trailing empties dropped, negative for no limit with them kept- Returns:
- the pieces
-
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
Returns a predicate answering whether the pattern matches a whole string.- Returns:
- a predicate answering whether the pattern matches a whole string
-
splitAsStream
The pieces an input splits into, as a stream.- Parameters:
input- what to split- Returns:
- the pieces
-
namedGroups
Returns every named group, in the order the pattern declares them.- Returns:
- every named group, in the order the pattern declares them
-