1. /*
  2. * @(#)MatchResult.java 1.5 04/06/28
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.regex;
  8. /**
  9. * The result of a match operation.
  10. *
  11. * <p>This interface contains query methods used to determine the
  12. * results of a match against a regular expression. The match boundaries,
  13. * groups and group boundaries can be seen but not modified through
  14. * a <code>MatchResult</code>.
  15. *
  16. * @author Michael McCloskey
  17. * @version 1.5 06/28/04
  18. * @see Matcher
  19. * @since 1.5
  20. */
  21. public interface MatchResult {
  22. /**
  23. * Returns the start index of the match.
  24. *
  25. * @return The index of the first character matched
  26. *
  27. * @throws IllegalStateException
  28. * If no match has yet been attempted,
  29. * or if the previous match operation failed
  30. */
  31. public int start();
  32. /**
  33. * Returns the start index of the subsequence captured by the given group
  34. * during this match.
  35. *
  36. * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
  37. * to right, starting at one. Group zero denotes the entire pattern, so
  38. * the expression <i>m.</i><tt>start(0)</tt> is equivalent to
  39. * <i>m.</i><tt>start()</tt>. </p>
  40. *
  41. * @param group
  42. * The index of a capturing group in this matcher's pattern
  43. *
  44. * @return The index of the first character captured by the group,
  45. * or <tt>-1</tt> if the match was successful but the group
  46. * itself did not match anything
  47. *
  48. * @throws IllegalStateException
  49. * If no match has yet been attempted,
  50. * or if the previous match operation failed
  51. *
  52. * @throws IndexOutOfBoundsException
  53. * If there is no capturing group in the pattern
  54. * with the given index
  55. */
  56. public int start(int group);
  57. /**
  58. * Returns the offset after the last character matched. </p>
  59. *
  60. * @return @return The offset after the last character matched
  61. *
  62. * @throws IllegalStateException
  63. * If no match has yet been attempted,
  64. * or if the previous match operation failed
  65. */
  66. public int end();
  67. /**
  68. * Returns the offset after the last character of the subsequence
  69. * captured by the given group during this match.
  70. *
  71. * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
  72. * to right, starting at one. Group zero denotes the entire pattern, so
  73. * the expression <i>m.</i><tt>end(0)</tt> is equivalent to
  74. * <i>m.</i><tt>end()</tt>. </p>
  75. *
  76. * @param group
  77. * The index of a capturing group in this matcher's pattern
  78. *
  79. * @return The offset after the last character captured by the group,
  80. * or <tt>-1</tt> if the match was successful
  81. * but the group itself did not match anything
  82. *
  83. * @throws IllegalStateException
  84. * If no match has yet been attempted,
  85. * or if the previous match operation failed
  86. *
  87. * @throws IndexOutOfBoundsException
  88. * If there is no capturing group in the pattern
  89. * with the given index
  90. */
  91. public int end(int group);
  92. /**
  93. * Returns the input subsequence matched by the previous match.
  94. *
  95. * <p> For a matcher <i>m</i> with input sequence <i>s</i>,
  96. * the expressions <i>m.</i><tt>group()</tt> and
  97. * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(),</tt> <i>m.</i><tt>end())</tt>
  98. * are equivalent. </p>
  99. *
  100. * <p> Note that some patterns, for example <tt>a*</tt>, match the empty
  101. * string. This method will return the empty string when the pattern
  102. * successfully matches the empty string in the input. </p>
  103. *
  104. * @return The (possibly empty) subsequence matched by the previous match,
  105. * in string form
  106. *
  107. * @throws IllegalStateException
  108. * If no match has yet been attempted,
  109. * or if the previous match operation failed
  110. */
  111. public String group();
  112. /**
  113. * Returns the input subsequence captured by the given group during the
  114. * previous match operation.
  115. *
  116. * <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index
  117. * <i>g</i>, the expressions <i>m.</i><tt>group(</tt><i>g</i><tt>)</tt> and
  118. * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(</tt><i>g</i><tt>),</tt> <i>m.</i><tt>end(</tt><i>g</i><tt>))</tt>
  119. * are equivalent. </p>
  120. *
  121. * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
  122. * to right, starting at one. Group zero denotes the entire pattern, so
  123. * the expression <tt>m.group(0)</tt> is equivalent to <tt>m.group()</tt>.
  124. * </p>
  125. *
  126. * <p> If the match was successful but the group specified failed to match
  127. * any part of the input sequence, then <tt>null</tt> is returned. Note
  128. * that some groups, for example <tt>(a*)</tt>, match the empty string.
  129. * This method will return the empty string when such a group successfully
  130. * matches the empty string in the input. </p>
  131. *
  132. * @param group
  133. * The index of a capturing group in this matcher's pattern
  134. *
  135. * @return The (possibly empty) subsequence captured by the group
  136. * during the previous match, or <tt>null</tt> if the group
  137. * failed to match part of the input
  138. *
  139. * @throws IllegalStateException
  140. * If no match has yet been attempted,
  141. * or if the previous match operation failed
  142. *
  143. * @throws IndexOutOfBoundsException
  144. * If there is no capturing group in the pattern
  145. * with the given index
  146. */
  147. public String group(int group);
  148. /**
  149. * Returns the number of capturing groups in this match result's pattern.
  150. *
  151. * <p> Group zero denotes the entire pattern by convention. It is not
  152. * included in this count.
  153. *
  154. * <p> Any non-negative integer smaller than or equal to the value
  155. * returned by this method is guaranteed to be a valid group index for
  156. * this matcher. </p>
  157. *
  158. * @return The number of capturing groups in this matcher's pattern
  159. */
  160. public int groupCount();
  161. }