public class SearchAndReplaceBuilder
extends java.lang.Object
Given a set of patterns and associated StringReplacement
functions, you can produce an
optimized, thread-safe Function<String,String> that will find all occurrences of those patterns and replace
them with their replacements.
The returned function is thread-safe.
NOTE that building a search and replace function is a relatively complex procedure. You should typically do it only once for each pattern set you want to use. Usually you would do this in a static initializer.
You can provide a cache that can remember and recall built functions, which allows you to build them during your build process in various ways, instead of building them at runtime. Or you can use the cache to store built functions on the first run of your program so they don't need to be built the next time... But this is usually unnecessary, since building them is more than fast enough to do during runtime initialization.
Constructor and Description |
---|
SearchAndReplaceBuilder()
Create a new SearchAndReplaceBuilder without a
BuilderCache |
SearchAndReplaceBuilder(BuilderCache cache)
Create a new SearchAndReplaceBuilder, with a builder cache to bypass recalculation of pre-built functions
|
Modifier and Type | Method and Description |
---|---|
SearchAndReplaceBuilder |
addIgnorePattern(Matchable pat)
Add a pattern to ignore
|
SearchAndReplaceBuilder |
addReplacement(Matchable pat,
StringReplacement replacement)
Add a dynamic search + replacement.
|
SearchAndReplaceBuilder |
addStringReplacement(Matchable pat,
java.lang.CharSequence replacement)
Add a search + string replacement.
|
static <MR> java.util.function.Function<java.lang.String,java.lang.String> |
buildFromSearcher(StringSearcher<MR> searcher,
ReplacementSelector<? super MR> replacer)
Build a search and replace function from a searcher and replacer
|
java.util.function.Function<java.lang.String,java.lang.String> |
buildStringReplacer()
Build a search and replace function
|
void |
clear()
Reset this builder by forgetting all the patterns that have been added
|
public SearchAndReplaceBuilder()
BuilderCache
public SearchAndReplaceBuilder(BuilderCache cache)
cache
- The BuilderCache to usepublic void clear()
public SearchAndReplaceBuilder addStringReplacement(Matchable pat, java.lang.CharSequence replacement)
Occurrences of the search pattern will be replaced with the given string.
This is equivalent to addReplacement(pat, StringReplacements.string(replacement));
pat
- The pattern to search forreplacement
- A function to generate the replacement valuepublic SearchAndReplaceBuilder addReplacement(Matchable pat, StringReplacement replacement)
The provided replacement function will be called to generate the replacement value for each occurrence of the search pattern.
StringReplacements
contains commonly used replacement functions
pat
- The pattern to search forreplacement
- A function to generate the replacement valuepublic SearchAndReplaceBuilder addIgnorePattern(Matchable pat)
Occurrences of the search pattern will be left alone. This just adds a replacer that replaces occurrences of the search pattern with the same string.
With careful attention to match priority rules (see buildStringReplacer()
, this can be used for many
special purposes.
This is equivalent to addReplacement(pat, StringReplacements.IGNORE);
pat
- The pattern to search forpublic java.util.function.Function<java.lang.String,java.lang.String> buildStringReplacer()
The resulting function finds all patterns in the string you give it, and replaces them all with the associated replacement.
Matches are found in order of their start positions. If matches to more than one pattern occur at the same position, then the longest match will be used. If there is a tie, then the first one added to this builder will be used.
public static <MR> java.util.function.Function<java.lang.String,java.lang.String> buildFromSearcher(StringSearcher<MR> searcher, ReplacementSelector<? super MR> replacer)
searcher
- the searcherreplacer
- the replacer