1127 * The character sequence to be matched
1128 * @return whether or not the regular expression matches on the input
1129 * @throws PatternSyntaxException
1130 * If the expression's syntax is invalid
1131 */
1132 public static boolean matches(String regex, CharSequence input) {
1133 Pattern p = Pattern.compile(regex);
1134 Matcher m = p.matcher(input);
1135 return m.matches();
1136 }
1137
1138 /**
1139 * Splits the given input sequence around matches of this pattern.
1140 *
1141 * <p> The array returned by this method contains each substring of the
1142 * input sequence that is terminated by another subsequence that matches
1143 * this pattern or is terminated by the end of the input sequence. The
1144 * substrings in the array are in the order in which they occur in the
1145 * input. If this pattern does not match any subsequence of the input then
1146 * the resulting array has just one element, namely the input sequence in
1147 * string form.
1148 *
1149 * <p> The <tt>limit</tt> parameter controls the number of times the
1150 * pattern is applied and therefore affects the length of the resulting
1151 * array. If the limit <i>n</i> is greater than zero then the pattern
1152 * will be applied at most <i>n</i> - 1 times, the array's
1153 * length will be no greater than <i>n</i>, and the array's last entry
1154 * will contain all input beyond the last matched delimiter. If <i>n</i>
1155 * is non-positive then the pattern will be applied as many times as
1156 * possible and the array can have any length. If <i>n</i> is zero then
1157 * the pattern will be applied as many times as possible, the array can
1158 * have any length, and trailing empty strings will be discarded.
1159 *
1160 * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
1161 * results with these parameters:
1162 *
1163 * <blockquote><table cellpadding=1 cellspacing=0
1164 * summary="Split examples showing regex, limit, and result">
1165 * <tr><th align="left"><i>Regex </i></th>
1166 * <th align="left"><i>Limit </i></th>
1167 * <th align="left"><i>Result </i></th></tr>
1168 * <tr><td align=center>:</td>
1169 * <td align=center>2</td>
1170 * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
1171 * <tr><td align=center>:</td>
1172 * <td align=center>5</td>
1173 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1174 * <tr><td align=center>:</td>
1175 * <td align=center>-2</td>
1176 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1177 * <tr><td align=center>o</td>
1178 * <td align=center>5</td>
1179 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1180 * <tr><td align=center>o</td>
1181 * <td align=center>-2</td>
1182 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1183 * <tr><td align=center>o</td>
1184 * <td align=center>0</td>
1185 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
1186 * </table></blockquote>
1187 *
1188 *
1189 * @param input
1190 * The character sequence to be split
1191 *
1192 * @param limit
1193 * The result threshold, as described above
1194 *
1195 * @return The array of strings computed by splitting the input
1196 * around matches of this pattern
1197 */
1198 public String[] split(CharSequence input, int limit) {
1199 int index = 0;
1200 boolean matchLimited = limit > 0;
1201 ArrayList<String> matchList = new ArrayList<>();
1202 Matcher m = matcher(input);
1203
1204 // Add segments before each match found
1205 while(m.find()) {
1206 if (!matchLimited || matchList.size() < limit - 1) {
1207 String match = input.subSequence(index, m.start()).toString();
1208 matchList.add(match);
1209 index = m.end();
1210 } else if (matchList.size() == limit - 1) { // last one
1211 String match = input.subSequence(index,
1212 input.length()).toString();
1213 matchList.add(match);
1214 index = m.end();
1215 }
1216 }
1217
1218 // If no match was found, return this
1219 if (index == 0)
1220 return new String[] {input.toString()};
1221
1222 // Add remaining segment
1223 if (!matchLimited || matchList.size() < limit)
1224 matchList.add(input.subSequence(index, input.length()).toString());
1225
1226 // Construct result
5745 */
5746 public Predicate<String> asPredicate() {
5747 return s -> matcher(s).find();
5748 }
5749
5750 /**
5751 * Creates a stream from the given input sequence around matches of this
5752 * pattern.
5753 *
5754 * <p> The stream returned by this method contains each substring of the
5755 * input sequence that is terminated by another subsequence that matches
5756 * this pattern or is terminated by the end of the input sequence. The
5757 * substrings in the stream are in the order in which they occur in the
5758 * input. Trailing empty strings will be discarded and not encountered in
5759 * the stream.
5760 *
5761 * <p> If this pattern does not match any subsequence of the input then
5762 * the resulting stream has just one element, namely the input sequence in
5763 * string form.
5764 *
5765 * <p> If the input sequence is mutable, it must remain constant during the
5766 * execution of the terminal stream operation. Otherwise, the result of the
5767 * terminal stream operation is undefined.
5768 *
5769 * @param input
5770 * The character sequence to be split
5771 *
5772 * @return The stream of strings computed by splitting the input
5773 * around matches of this pattern
5774 * @see #split(CharSequence)
5775 * @since 1.8
5776 */
5777 public Stream<String> splitAsStream(final CharSequence input) {
5778 class MatcherIterator implements Iterator<String> {
5779 private final Matcher matcher;
5780 // The start position of the next sub-sequence of input
5781 // when current == input.length there are no more elements
5782 private int current;
5783 // null if the next element, if any, needs to obtained
5784 private String nextElement;
5800 } else {
5801 emptyElementCount--;
5802 return "";
5803 }
5804 }
5805
5806 public boolean hasNext() {
5807 if (nextElement != null || emptyElementCount > 0)
5808 return true;
5809
5810 if (current == input.length())
5811 return false;
5812
5813 // Consume the next matching element
5814 // Count sequence of matching empty elements
5815 while (matcher.find()) {
5816 nextElement = input.subSequence(current, matcher.start()).toString();
5817 current = matcher.end();
5818 if (!nextElement.isEmpty()) {
5819 return true;
5820 } else {
5821 emptyElementCount++;
5822 }
5823 }
5824
5825 // Consume last matching element
5826 nextElement = input.subSequence(current, input.length()).toString();
5827 current = input.length();
5828 if (!nextElement.isEmpty()) {
5829 return true;
5830 } else {
5831 // Ignore a terminal sequence of matching empty elements
5832 emptyElementCount = 0;
5833 nextElement = null;
5834 return false;
5835 }
5836 }
5837 }
5838 return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
5839 new MatcherIterator(), Spliterator.ORDERED | Spliterator.NONNULL), false);
5840 }
|
1127 * The character sequence to be matched
1128 * @return whether or not the regular expression matches on the input
1129 * @throws PatternSyntaxException
1130 * If the expression's syntax is invalid
1131 */
1132 public static boolean matches(String regex, CharSequence input) {
1133 Pattern p = Pattern.compile(regex);
1134 Matcher m = p.matcher(input);
1135 return m.matches();
1136 }
1137
1138 /**
1139 * Splits the given input sequence around matches of this pattern.
1140 *
1141 * <p> The array returned by this method contains each substring of the
1142 * input sequence that is terminated by another subsequence that matches
1143 * this pattern or is terminated by the end of the input sequence. The
1144 * substrings in the array are in the order in which they occur in the
1145 * input. If this pattern does not match any subsequence of the input then
1146 * the resulting array has just one element, namely the input sequence in
1147 * string form. A zero-length input sequence always results zero-length
1148 * resulting array.
1149 *
1150 * <p> When there is a positive-width match at the beginning of the input
1151 * sequence then an empty leading substring is included at the beginning
1152 * of the resulting array. A zero-width match at the beginning however
1153 * never produces such empty leading substring.
1154 *
1155 * <p> The <tt>limit</tt> parameter controls the number of times the
1156 * pattern is applied and therefore affects the length of the resulting
1157 * array. If the limit <i>n</i> is greater than zero then the pattern
1158 * will be applied at most <i>n</i> - 1 times, the array's
1159 * length will be no greater than <i>n</i>, and the array's last entry
1160 * will contain all input beyond the last matched delimiter. If <i>n</i>
1161 * is non-positive then the pattern will be applied as many times as
1162 * possible and the array can have any length. If <i>n</i> is zero then
1163 * the pattern will be applied as many times as possible, the array can
1164 * have any length, and trailing empty strings will be discarded.
1165 *
1166 * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
1167 * results with these parameters:
1168 *
1169 * <blockquote><table cellpadding=1 cellspacing=0
1170 * summary="Split examples showing regex, limit, and result">
1171 * <tr><th align="left"><i>Regex </i></th>
1172 * <th align="left"><i>Limit </i></th>
1173 * <th align="left"><i>Result </i></th></tr>
1174 * <tr><td align=center>:</td>
1175 * <td align=center>2</td>
1176 * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
1177 * <tr><td align=center>:</td>
1178 * <td align=center>5</td>
1179 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1180 * <tr><td align=center>:</td>
1181 * <td align=center>-2</td>
1182 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1183 * <tr><td align=center>o</td>
1184 * <td align=center>5</td>
1185 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1186 * <tr><td align=center>o</td>
1187 * <td align=center>-2</td>
1188 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1189 * <tr><td align=center>o</td>
1190 * <td align=center>0</td>
1191 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
1192 * </table></blockquote>
1193 *
1194 * @param input
1195 * The character sequence to be split
1196 *
1197 * @param limit
1198 * The result threshold, as described above
1199 *
1200 * @return The array of strings computed by splitting the input
1201 * around matches of this pattern
1202 */
1203 public String[] split(CharSequence input, int limit) {
1204 if (input.length() == 0)
1205 return new String[0];
1206 int index = 0;
1207 boolean matchLimited = limit > 0;
1208 ArrayList<String> matchList = new ArrayList<>();
1209 Matcher m = matcher(input);
1210
1211 // Add segments before each match found
1212 while(m.find()) {
1213 if (!matchLimited || matchList.size() < limit - 1) {
1214 if (index == 0 && index == m.start() && m.start() == m.end()) {
1215 // no empty leading substring included for zero-width match
1216 // at the beginning of the input char sequence.
1217 continue;
1218 }
1219 String match = input.subSequence(index, m.start()).toString();
1220 matchList.add(match);
1221 index = m.end();
1222 } else if (matchList.size() == limit - 1) { // last one
1223 String match = input.subSequence(index,
1224 input.length()).toString();
1225 matchList.add(match);
1226 index = m.end();
1227 }
1228 }
1229
1230 // If no match was found, return this
1231 if (index == 0)
1232 return new String[] {input.toString()};
1233
1234 // Add remaining segment
1235 if (!matchLimited || matchList.size() < limit)
1236 matchList.add(input.subSequence(index, input.length()).toString());
1237
1238 // Construct result
5757 */
5758 public Predicate<String> asPredicate() {
5759 return s -> matcher(s).find();
5760 }
5761
5762 /**
5763 * Creates a stream from the given input sequence around matches of this
5764 * pattern.
5765 *
5766 * <p> The stream returned by this method contains each substring of the
5767 * input sequence that is terminated by another subsequence that matches
5768 * this pattern or is terminated by the end of the input sequence. The
5769 * substrings in the stream are in the order in which they occur in the
5770 * input. Trailing empty strings will be discarded and not encountered in
5771 * the stream.
5772 *
5773 * <p> If this pattern does not match any subsequence of the input then
5774 * the resulting stream has just one element, namely the input sequence in
5775 * string form.
5776 *
5777 * <p> A zero-length input sequence always results an empty stream.
5778 *
5779 * <p> When there is a positive-width match at the beginning of the input
5780 * sequence then an empty leading substring is included at the beginning
5781 * of the stream. A zero-width match at the beginning however never produces
5782 * such empty leading substring.
5783 *
5784 * <p> If the input sequence is mutable, it must remain constant during the
5785 * execution of the terminal stream operation. Otherwise, the result of the
5786 * terminal stream operation is undefined.
5787 *
5788 * @param input
5789 * The character sequence to be split
5790 *
5791 * @return The stream of strings computed by splitting the input
5792 * around matches of this pattern
5793 * @see #split(CharSequence)
5794 * @since 1.8
5795 */
5796 public Stream<String> splitAsStream(final CharSequence input) {
5797 class MatcherIterator implements Iterator<String> {
5798 private final Matcher matcher;
5799 // The start position of the next sub-sequence of input
5800 // when current == input.length there are no more elements
5801 private int current;
5802 // null if the next element, if any, needs to obtained
5803 private String nextElement;
5819 } else {
5820 emptyElementCount--;
5821 return "";
5822 }
5823 }
5824
5825 public boolean hasNext() {
5826 if (nextElement != null || emptyElementCount > 0)
5827 return true;
5828
5829 if (current == input.length())
5830 return false;
5831
5832 // Consume the next matching element
5833 // Count sequence of matching empty elements
5834 while (matcher.find()) {
5835 nextElement = input.subSequence(current, matcher.start()).toString();
5836 current = matcher.end();
5837 if (!nextElement.isEmpty()) {
5838 return true;
5839 } else if (current > 0) { // no empty leading substring for zero-width
5840 // match at the beginning of the input
5841 emptyElementCount++;
5842 }
5843 }
5844
5845 // Consume last matching element
5846 nextElement = input.subSequence(current, input.length()).toString();
5847 current = input.length();
5848 if (!nextElement.isEmpty()) {
5849 return true;
5850 } else {
5851 // Ignore a terminal sequence of matching empty elements
5852 emptyElementCount = 0;
5853 nextElement = null;
5854 return false;
5855 }
5856 }
5857 }
5858 return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
5859 new MatcherIterator(), Spliterator.ORDERED | Spliterator.NONNULL), false);
5860 }
|