Class Substring.RepeatingPattern
- Enclosing class:
Substring
- Since:
- 5.2
-
Method Summary
Modifier and TypeMethodDescriptionalternationFrom
(String input) Returns the alternation of this pattern from theinput
string, with the matched substring alternated with the trailing substring before the next match.Returns a stream ofMatch
objects from the inputstring
as demarcated by this delimiter pattern.from
(CharSequence input) Applies this pattern againststring
and returns a stream of each iteration.final Stream
<Substring.Match> Applies this pattern againststring
and returns a stream of each iteration.abstract Stream
<Substring.Match> Applies this pattern againststring
starting fromfromIndex
and returns a stream of each iteration.removeAllFrom
(String string) Returns a new string with allmatches
of this pattern removed.replaceAllFrom
(String string, Function<? super Substring.Match, ? extends CharSequence> replacementFunction) Returns a new string with allmatches
of this pattern replaced by applyingreplacementFunction
for each match.Returns a stream ofMatch
objects delimited by every match of this pattern.splitKeyValuesAround
(Substring.Pattern keyValueSeparator, String input) Returns aBiStream
of key value pairs frominput
.splitThenTrim
(String string) Returns a stream ofMatch
objects delimited by every match of this pattern.splitThenTrimKeyValuesAround
(Substring.Pattern keyValueSeparator, String input) Returns aBiStream
of key value pairs frominput
.
-
Method Details
-
match
Applies this pattern againststring
starting fromfromIndex
and returns a stream of each iteration.Iterations happen in strict character encounter order, from the beginning of the input string to the end, with no overlapping. When a match is found, the next iteration is guaranteed to be in the substring after the current match. For example,
between(first('/'), first('/')).repeatedly().match("/foo/bar/baz/")
will return["foo", "bar", "baz"]
. On the other hand,after(last('/')).repeatedly().match("/foo/bar")
will only return "bar".Pattern matching is lazy and doesn't start until the returned stream is consumed.
An empty stream is returned if this pattern has no matches in the
input
string.- Throws:
IndexOutOfBoundsException
- iffromIndex
is negative or greater thaninput.length()
- Since:
- 8.2
-
match
Applies this pattern againststring
and returns a stream of each iteration.Iterations happen in strict character encounter order, from the beginning of the input string to the end, with no overlapping. When a match is found, the next iteration is guaranteed to be in the substring after the current match. For example,
between(first('/'), first('/')).repeatedly().match("/foo/bar/baz/")
will return["foo", "bar", "baz"]
. On the other hand,after(last('/')).repeatedly().match("/foo/bar")
will only return "bar".Pattern matching is lazy and doesn't start until the returned stream is consumed.
An empty stream is returned if this pattern has no matches in the
input
string. -
from
Applies this pattern againststring
and returns a stream of each iteration.Iterations happen in strict character encounter order, from the beginning of the input string to the end, with no overlapping. When a match is found, the next iteration is guaranteed to be in the substring after the current match. For example,
between(first('/'), first('/')).repeatedly().from("/foo/bar/baz/")
will return["foo", "bar", "baz"]
. On the other hand,after(last('/')).repeatedly().from("/foo/bar")
will only return "bar".Pattern matching is lazy and doesn't start until the returned stream is consumed.
An empty stream is returned if this pattern has no matches in the
input
string. -
removeAllFrom
Returns a new string with allmatches
of this pattern removed. Returnsstring
as is if no match is found. -
replaceAllFrom
public String replaceAllFrom(String string, Function<? super Substring.Match, ? extends CharSequence> replacementFunction) Returns a new string with allmatches
of this pattern replaced by applyingreplacementFunction
for each match.replacementFunction
must not return null. Returnsstring
as-is if no match is found. -
split
Returns a stream ofMatch
objects delimited by every match of this pattern. If this pattern isn't found instring
, the full string is matched.The returned
Match
objects are cheap "views" of the matched substring sequences. BecauseMatch
implementsCharSequence
, the returnedMatch
objects can be directly passed toCharSequence
-accepting APIs such ascom.google.common.base.CharMatcher.trimFrom()
andSubstring.Pattern.splitThenTrim(java.lang.CharSequence)
etc. -
splitThenTrim
Returns a stream ofMatch
objects delimited by every match of this pattern. with whitespaces trimmed.The returned
Match
objects are cheap "views" of the matched substring sequences. BecauseMatch
implementsCharSequence
, the returnedMatch
objects can be directly passed toCharSequence
-accepting APIs such ascom.google.common.base.CharMatcher.trimFrom()
andSubstring.Pattern.split(java.lang.CharSequence)
etc. -
cut
Returns a stream ofMatch
objects from the inputstring
as demarcated by this delimiter pattern. It's similar tosplit(java.lang.String)
but includes both the substrings split by the delimiters and the delimiter substrings themselves, interpolated in the order they appear in the input string.For example,
will result in the stream ofspanningInOrder("{", "}").repeatedly().cut("Dear {user}: please {act}.")
["Dear ", "{user}", ": please ", "{act}", "."]
.The returned
Match
objects are cheap "views" of the matched substring sequences. BecauseMatch
implementsCharSequence
, the returnedMatch
objects can be directly passed toCharSequence
-accepting APIs such as GuavaCharMatcher.trimFrom
,Substring.Pattern.splitThenTrim(java.lang.CharSequence)
, etc.- Since:
- 7.1
-
splitKeyValuesAround
public final BiStream<String,String> splitKeyValuesAround(Substring.Pattern keyValueSeparator, String input) Returns aBiStream
of key value pairs frominput
.The key-value pairs are delimited by this repeating pattern. with the key and value separated by
keyValueSeparator
.Empty parts (including leading and trailing separator) are ignored. Although whitespaces are not trimmed. For example:
will result in afirst(',') .repeatedly() .splitKeyValuesAround(first('='), "k1=v1,,k2=v2,")
BiStream
equivalent to[(k1, v1), (k2, v2)]
, but"k1=v1, ,k2=v2"
will fail to be split due to the whitespace after the first','
.Non-empty parts where
keyValueSeparator
is absent will result inIllegalArgumentException
.For alternative splitting strategies, like, if you want to reject instead of ignoring empty parts. consider to use
split(java.lang.String)
andSubstring.Pattern.split(java.lang.CharSequence)
directly, such as:
Or, if you want to ignore unparsable parts:first(',') .repeatedly() .split("k1=v1,,k2=v2,") // the redundant ',' will throw IAE .collect( GuavaCollectors.toImmutableMap( m -> first('=').split(m).orElseThrow(...)));
first(',') .repeatedly() .split("k1=v1,k2>v2") // Ignore the unknown "k2>v2" .map(first('=')::split) .collect( MoreCollectors.flatMapping( BiOptional::stream, toImmutableMap()));
- Since:
- 5.9
-
splitThenTrimKeyValuesAround
public final BiStream<String,String> splitThenTrimKeyValuesAround(Substring.Pattern keyValueSeparator, String input) Returns aBiStream
of key value pairs frominput
.The key-value pairs are delimited by this repeating pattern. with the key and value separated by
keyValueSeparator
.All keys and values are trimmed, with empty parts (including leading and trailing separator) ignored. For example:
will result in afirst(',') .repeatedly() .splitThenTrimKeyValuesAround(first('='), "k1 = v1, , k2=v2,")
BiStream
equivalent to[(k1, v1), (k2, v2)]
.Non-empty parts where
keyValueSeparator
is absent will result inIllegalArgumentException
.For alternative splitting strategies, like, if you want to reject instead of ignoring empty parts. consider to use
split(java.lang.String)
andSubstring.Pattern.splitThenTrim(java.lang.CharSequence)
directly, such as:
Or, if you want to ignore unparsable parts:first(',') .repeatedly() .split("k1 = v1, , k2=v2,") // the redundant ',' will throw IAE .collect( GuavaCollectors.toImmutableMap( m -> first('=').splitThenTrim(m).orElseThrow(...)));
first(',') .repeatedly() .split("k1 = v1, k2 > v2") // Ignore the unknown "k2 > v2" .map(first('=')::splitThenTrim) .collect( MoreCollectors.flatMapping( BiOptional::stream, toImmutableMap()));
- Since:
- 5.9
-
alternationFrom
Returns the alternation of this pattern from theinput
string, with the matched substring alternated with the trailing substring before the next match.For example: to find bulleted items (strings prefixed by
1:
,2:
,456:
etc.), you can:Substring.Pattern bulletNumber = consecutive(CharPredicate.range('0', '9')) .separatedBy(CharPredicate.WORD.not(), CharPredicate.is(':')); Map<Integer, String> bulleted = bulletNumber.repeatedly() .alternationFrom("1: go home;2: feed 2 cats 3: sleep tight.") .mapKeys(n -> Integer.parseInt(n)) .mapValues(withColon -> prefix(":").removeFrom(withColon.toString()).trim()) .toMap(); // => [{1, "go home;"}, {2, "feed 2 cats"}, {3, "sleep tight."}]
- Since:
- 6.1
-