public final class ReplacedText extends TextEnvelope
Text.NoNulls
public ReplacedText(Text text, String find, String replace)
Will replace all instances of the substring matched by find
with replace
.
text
- The textfind
- The regular expressionreplace
- The replacement stringpublic ReplacedText(Text text, Scalar<Pattern> regex, Func<Matcher,String> func)
The given regex
is used to produce a
matcher
that will be
transformed by func
into a replacement string to replace each
matching
substring.
Example usage:
final String result = new ReplacedText(
new TextOf("one two THREE four FIVE six"),
() -> Pattern.compile("[a-z]+"),
matcher -> String.valueOf(matcher.group().length())
).asString(); //will return the string "3 3 THREE 4 FIVE 3"
Note: a PatternSyntaxException
will be thrown if the
regular expression's syntax is invalid.
text
- The textregex
- The regular expressionfunc
- Transforms the resulting matcher object into a replacement
string. Any exceptions will be wrapped in an IOException
.Copyright © 2017–2018 Cactoos. All rights reserved.