public final class PatternUtil
extends java.lang.Object
Pattern-related utility methods.| Modifier and Type | Method and Description |
|---|---|
static Function<java.util.regex.Matcher,java.lang.String> |
constantReplacer(java.lang.String string)
Return a replacer which always returns the given string.
|
static java.lang.String |
replaceAll(java.util.regex.Matcher matcher,
Function<java.util.regex.Matcher,java.lang.String> replacer)
The generalized form of
Matcher.replaceAll(String): The replacement for a match is not formed from a
"replacement string" (with variables "$0", "$1", ...), but is computed by the replacer. |
static int |
replaceAll(java.io.Reader in,
java.util.regex.Pattern pattern,
Function<java.util.regex.Matcher,java.lang.String> replacer,
java.io.Writer out,
int initialBufferCapacity)
Reads characters from in, finds all matches of pattern, replaces each match with the
result of the replacer, and writes the result to out.
|
static void |
replaceAll(java.io.Reader in,
java.util.regex.Pattern pattern,
java.lang.String replacementString,
java.io.Writer out)
Reads text from in, replaces all matches of pattern according to the
replacementString, and writes the result to out.
|
static Function<java.util.regex.Matcher,java.lang.String> |
replacementStringReplacer(java.lang.String replacementString) |
static int |
replaceSystemProperties(java.io.Reader in,
java.io.Writer out)
Reads text from in, replaces all matches of
"
${system-property-name}" with the value of the system property, and writes
the result to out. |
static Function<java.util.regex.Matcher,java.lang.String> |
systemPropertyReplacer()
Returns a replacer which returns the value of the system property named by group #1 of the match.
|
public static void replaceAll(java.io.Reader in,
java.util.regex.Pattern pattern,
java.lang.String replacementString,
java.io.Writer out)
throws java.io.IOException
The pattern search is stream-oriented, not line-oriented, i.e. matches are found even across line boundaries.
Thus the pattern should have been compiled with the Pattern.MULTILINE flag.
java.io.IOExceptionFor the format of the replacementStringpublic static Function<java.util.regex.Matcher,java.lang.String> constantReplacer(@Nullable java.lang.String string)
Opposed to replacementStringReplacer(String), "$" and "\" have no special meaning.
public static Function<java.util.regex.Matcher,java.lang.String> replacementStringReplacer(java.lang.String replacementString)
For the format of the replacementString,
constantReplacer(String),
systemPropertyReplacer()public static Function<java.util.regex.Matcher,java.lang.String> systemPropertyReplacer()
Example:
PatternUtil.replaceAll(
Pattern.compile("\\$\\{([^}]+)}").matcher("file.separator is ${file.separator}"),
PatternUtil.systemPropertyReplacer()
)
public static int replaceSystemProperties(java.io.Reader in,
java.io.Writer out)
throws java.io.IOException
${system-property-name}" with the value of the system property, and writes
the result to out.java.io.IOExceptionpublic static java.lang.String replaceAll(java.util.regex.Matcher matcher,
Function<java.util.regex.Matcher,java.lang.String> replacer)
Matcher.replaceAll(String): The replacement for a match is not formed from a
"replacement string" (with variables "$0", "$1", ...), but is computed by the replacer. If the
replacer returns null for a match, then the match is not replaced.public static int replaceAll(java.io.Reader in,
java.util.regex.Pattern pattern,
Function<java.util.regex.Matcher,java.lang.String> replacer,
java.io.Writer out,
int initialBufferCapacity)
throws java.io.IOException
null
for a match, then the match is not replaced.
The pattern search is stream-oriented, not line-oriented, i.e. matches are found even across line boundaries.
Therefore the pattern should have been compiled with the Pattern.MULTILINE flag.
This method attempts to load as few characters into memory as possible. Notice, however, that in particular
the usage of "greedy quantifiers", for example "a.*b", can lead to all the remaining content being
read into memory.
initialBufferCapacity - The initial capacity of the temporary CharBuffer that is used for pattern
matching; the buffer will automatically be resized as necessary; 4096 may be a good
valuejava.io.IOException