Class Substring.Match

java.lang.Object
com.google.mu.util.Substring.Match
All Implemented Interfaces:
CharSequence
Enclosing class:
Substring

public static final class Substring.Match extends Object implements CharSequence
The result of successfully matching a Substring.Pattern against a string, providing access to the matched substring, to the parts of the string before and after it, and to copies with the matched substring removed or replaced.

Note: a Substring.Match is a view of the original string and holds a strong reference to the original string. It's advisable to construct and use a Match object within the scope of a method; holding onto a Match object has the same risk of leaking memory as holding onto the string it was produced from.

  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the part of the original string before the matched substring.
    Returns the part of the original string before the matched substring.
    char
    charAt(int i)
    boolean
    Equivalent to toString().equals(str) but without copying the characters into a temporary string.
    boolean
    endsWith(String suffix)
    Returns true if this match ends with the given suffix.
    Return the full string being matched against.
    int
    Return 0-based index of this match in fullString().
    boolean
    Returns true if the match is empty.
    boolean
    isFollowedBy(String lookahead)
    Returns true if the match is immediately followed by the lookahead string.
    boolean
    isImmediatelyBetween(String lookbehind, String lookahead)
    Returns true if the match immediately follows the lookbehind string and is immediately followed by the lookahead string.
    boolean
    Returns true if the match isn't empty.
    boolean
    isPrecededBy(String lookbehind)
    Returns true if the match immediately follows the lookbehind string.
    int
    Returns the length of the matched substring.
    limit(int maxChars)
    Returns an equivalent match with at most maxChars.
    Returns a copy of the original string with the matched substring removed.
    Returns a copy of the original string with the matched substring replaced with replacement.
    skip(int fromBeginning, int fromEnd)
    Returns a new instance that's otherwise equivalent except with fromBeginning characters skipped from the beginning and fromEnd characters skipped from the end.
    boolean
    Returns true if this match starts with the given prefix.
    subSequence(int begin, int end)
    Returns a CharSequence instance which is a sub-range of this Match.
    Returns the matched substring.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.CharSequence

    chars, codePoints
  • Method Details

    • before

      public String before()
      Returns the part of the original string before the matched substring.

      before() and after() are almost always used together to split a string into two parts. If you just need the substring before the match, you might want to use Substring.before(pattern) instead, because the pattern logic is encoded entirely in the Substring.Pattern object. For example:

         private static final Substring.Pattern DIRECTORY = Substring.before(last("/"));
       
    • after

      public String after()
      Returns the part of the original string before the matched substring.

      before() and after() are almost always used together to split a string into two parts. If you just need the substring after the match, you might want to use Substring.after(pattern) instead, because the pattern logic is encoded entirely in the Substring.Pattern object. For example:

         private static final Substring.Pattern LINE_COMMENT = Substring.after(first("//"));
       
    • fullString

      public String fullString()
      Return the full string being matched against.
    • remove

      public String remove()
      Returns a copy of the original string with the matched substring removed.

      This is equivalent to match.before() + match.after().

    • replaceWith

      public String replaceWith(CharSequence replacement)
      Returns a copy of the original string with the matched substring replaced with replacement.

      This is equivalent to match.before() + replacement + match.after().

    • isEmpty

      public boolean isEmpty()
      Returns true if the match is empty.
      Specified by:
      isEmpty in interface CharSequence
      Since:
      7.2
    • isNotEmpty

      public boolean isNotEmpty()
      Returns true if the match isn't empty.
      Since:
      6.0
    • limit

      public Substring.Match limit(int maxChars)
      Returns an equivalent match with at most maxChars.
      Since:
      6.1
    • skip

      public Substring.Match skip(int fromBeginning, int fromEnd)
      Returns a new instance that's otherwise equivalent except with fromBeginning characters skipped from the beginning and fromEnd characters skipped from the end. If there are fewer characters, an empty match is returned.

      For example, first("hello").in("say hello").get().skip(2, 1) returns "ll".

      Since:
      6.1
    • index

      public int index()
      Return 0-based index of this match in fullString().
    • contentEquals

      public boolean contentEquals(String str)
      Equivalent to toString().equals(str) but without copying the characters into a temporary string.
      Since:
      7.1
    • startsWith

      public boolean startsWith(String prefix)
      Returns true if this match starts with the given prefix.
      Since:
      7.0
    • endsWith

      public boolean endsWith(String suffix)
      Returns true if this match ends with the given suffix.
      Since:
      7.0
    • isFollowedBy

      public boolean isFollowedBy(String lookahead)
      Returns true if the match is immediately followed by the lookahead string. Note that isFollowedBy("") is always true.
    • isPrecededBy

      public boolean isPrecededBy(String lookbehind)
      Returns true if the match immediately follows the lookbehind string. Note that isPrecededBy("") is always true.
    • isImmediatelyBetween

      public boolean isImmediatelyBetween(String lookbehind, String lookahead)
      Returns true if the match immediately follows the lookbehind string and is immediately followed by the lookahead string. Note that isBetween("", "") is always true.
    • length

      public int length()
      Returns the length of the matched substring.
      Specified by:
      length in interface CharSequence
    • charAt

      public char charAt(int i)
      Specified by:
      charAt in interface CharSequence
      Since:
      4.6
    • subSequence

      public CharSequence subSequence(int begin, int end)
      Returns a CharSequence instance which is a sub-range of this Match.

      For example, if this Match points to the range of "wood" from the "Holywood" string, calling subSequence(1, 3) will point to the range of "oo" from the original string.

      Can be used to further reduce the matched range manually.

      Specified by:
      subSequence in interface CharSequence
      Since:
      4.6
    • toString

      public String toString()
      Returns the matched substring.
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object