Class Substring.RepeatingPattern
- Enclosing class:
Substring
- Since:
- 5.2
-
Method Summary
Modifier and TypeMethodDescriptionalternationFrom(String input) Returns the alternation of this pattern from theinputstring, with the matched substring alternated with the trailing substring before the next match.Returns a stream ofMatchobjects from the inputstringas demarcated by this delimiter pattern.from(CharSequence input) Applies this pattern againststringand returns a stream of each iteration.final Stream<Substring.Match> Applies this pattern againststringand returns a stream of each iteration.abstract Stream<Substring.Match> Applies this pattern againststringstarting fromfromIndexand returns a stream of each iteration.removeAllFrom(String string) Returns a new string with allmatchesof this pattern removed.replaceAllFrom(String string, Function<? super Substring.Match, ? extends CharSequence> replacementFunction) Returns a new string with allmatchesof this pattern replaced by applyingreplacementFunctionfor each match.Returns a stream ofMatchobjects delimited by every match of this pattern.splitKeyValuesAround(Substring.Pattern keyValueSeparator, String input) Returns aBiStreamof key value pairs frominput.splitThenTrim(String string) Returns a stream ofMatchobjects delimited by every match of this pattern.splitThenTrimKeyValuesAround(Substring.Pattern keyValueSeparator, String input) Returns aBiStreamof key value pairs frominput.
-
Method Details
-
match
Applies this pattern againststringstarting fromfromIndexand 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
inputstring.- Throws:
IndexOutOfBoundsException- iffromIndexis negative or greater thaninput.length()- Since:
- 8.2
-
match
Applies this pattern againststringand 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
inputstring. -
from
Applies this pattern againststringand 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
inputstring. -
removeAllFrom
-
replaceAllFrom
public String replaceAllFrom(String string, Function<? super Substring.Match, ? extends CharSequence> replacementFunction) Returns a new string with allmatchesof this pattern replaced by applyingreplacementFunctionfor each match.replacementFunctionmust not return null. Returnsstringas-is if no match is found. -
split
Returns a stream ofMatchobjects delimited by every match of this pattern. If this pattern isn't found instring, the full string is matched.The returned
Matchobjects are cheap "views" of the matched substring sequences. BecauseMatchimplementsCharSequence, the returnedMatchobjects can be directly passed toCharSequence-accepting APIs such ascom.google.common.base.CharMatcher.trimFrom()andSubstring.Pattern.splitThenTrim(java.lang.CharSequence, java.util.function.BiFunction<? super java.lang.String, ? super java.lang.String, ? extends T>)etc. -
splitThenTrim
Returns a stream ofMatchobjects delimited by every match of this pattern. with whitespaces trimmed.The returned
Matchobjects are cheap "views" of the matched substring sequences. BecauseMatchimplementsCharSequence, the returnedMatchobjects can be directly passed toCharSequence-accepting APIs such ascom.google.common.base.CharMatcher.trimFrom()andSubstring.Pattern.split(java.lang.CharSequence, java.util.function.BiFunction<? super java.lang.String, ? super java.lang.String, ? extends T>)etc. -
cut
Returns a stream ofMatchobjects from the inputstringas 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
Matchobjects are cheap "views" of the matched substring sequences. BecauseMatchimplementsCharSequence, the returnedMatchobjects can be directly passed toCharSequence-accepting APIs such as GuavaCharMatcher.trimFrom,Substring.Pattern.splitThenTrim(java.lang.CharSequence, java.util.function.BiFunction<? super java.lang.String, ? super java.lang.String, ? extends T>), etc.- Since:
- 7.1
-
splitKeyValuesAround
public final BiStream<String,String> splitKeyValuesAround(Substring.Pattern keyValueSeparator, String input) Returns aBiStreamof 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 aall(',').splitKeyValuesAround(first('='), "k1=v1,,k2=v2,")BiStreamequivalent 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
keyValueSeparatoris 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, java.util.function.BiFunction<? super java.lang.String, ? super java.lang.String, ? extends T>)directly, such as:
Or, if you want to ignore unparsable parts:all(',') .split("k1=v1,,k2=v2,") // the redundant ',' will throw IAE .collect( GuavaCollectors.toImmutableMap( m -> first('=').split(m).orElseThrow(...)));all(',') .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 aBiStreamof 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 aall(',').splitThenTrimKeyValuesAround(first('='), "k1 = v1, , k2=v2,")BiStreamequivalent to[(k1, v1), (k2, v2)].Non-empty parts where
keyValueSeparatoris 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, java.util.function.BiFunction<? super java.lang.String, ? super java.lang.String, ? extends T>)directly, such as:
Or, if you want to ignore unparsable parts:all(',') .split("k1 = v1, , k2=v2,") // the redundant ',' will throw IAE .collect( GuavaCollectors.toImmutableMap( m -> first('=').splitThenTrim(m).orElseThrow(...)));all(',') .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 theinputstring, 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
-