src/java.base/share/classes/java/util/regex/Matcher.java

Print this page




 976      * @param  sb
 977      *         The target string builder
 978      * @param  replacement
 979      *         The replacement string
 980      * @return  This matcher
 981      *
 982      * @throws  IllegalStateException
 983      *          If no match has yet been attempted,
 984      *          or if the previous match operation failed
 985      * @throws  IllegalArgumentException
 986      *          If the replacement string refers to a named-capturing
 987      *          group that does not exist in the pattern
 988      * @throws  IndexOutOfBoundsException
 989      *          If the replacement string refers to a capturing group
 990      *          that does not exist in the pattern
 991      * @since 9
 992      */
 993     public Matcher appendReplacement(StringBuilder sb, String replacement) {
 994         // If no match, return error
 995         if (first < 0)

 996             throw new IllegalStateException("No match available");
 997         StringBuilder result = new StringBuilder();
 998         appendExpandedReplacement(replacement, result);
 999         // Append the intervening text
1000         sb.append(text, lastAppendPosition, first);
1001         // Append the match substitution
1002         sb.append(result);
1003         lastAppendPosition = last;
1004         modCount++;
1005         return this;
1006     }
1007 
1008     /**
1009      * Processes replacement string to replace group references with
1010      * groups.
1011      */
1012     private StringBuilder appendExpandedReplacement(
1013         String replacement, StringBuilder result) {
1014         int cursor = 0;
1015         while (cursor < replacement.length()) {


1161      * {@code "-"}, an invocation of this method on a matcher for that
1162      * expression would yield the string {@code "-foo-foo-foo-"}.
1163      *
1164      * <p> Invoking this method changes this matcher's state.  If the matcher
1165      * is to be used in further matching operations then it should first be
1166      * reset.  </p>
1167      *
1168      * @param  replacement
1169      *         The replacement string
1170      *
1171      * @return  The string constructed by replacing each matching subsequence
1172      *          by the replacement string, substituting captured subsequences
1173      *          as needed
1174      */
1175     public String replaceAll(String replacement) {
1176         reset();
1177         boolean result = find();
1178         if (result) {
1179             StringBuilder sb = new StringBuilder();
1180             do {
1181                 appendReplacement(sb, replacement);


1182                 result = find();
1183             } while (result);
1184             appendTail(sb);
1185             return sb.toString();
1186         }
1187         return text.toString();
1188     }
1189 
1190     /**
1191      * Replaces every subsequence of the input sequence that matches the
1192      * pattern with the result of applying the given replacer function to the
1193      * match result of this matcher corresponding to that subsequence.
1194      * Exceptions thrown by the function are relayed to the caller.
1195      *
1196      * <p> This method first resets this matcher.  It then scans the input
1197      * sequence looking for matches of the pattern.  Characters that are not
1198      * part of any match are appended directly to the result string; each match
1199      * is replaced in the result by the applying the replacer function that
1200      * returns a replacement string.  Each replacement string may contain
1201      * references to captured subsequences as in the {@link #appendReplacement
1202      * appendReplacement} method.
1203      *
1204      * <p> Note that backslashes ({@code \}) and dollar signs ({@code $}) in


1387      * {@code "cat"}, an invocation of this method on a matcher for that
1388      * expression would yield the string {@code "zzzcatzzzdogzzz"}.  </p>
1389      *
1390      * <p> Invoking this method changes this matcher's state.  If the matcher
1391      * is to be used in further matching operations then it should first be
1392      * reset.  </p>
1393      *
1394      * @param  replacement
1395      *         The replacement string
1396      * @return  The string constructed by replacing the first matching
1397      *          subsequence by the replacement string, substituting captured
1398      *          subsequences as needed
1399      */
1400     public String replaceFirst(String replacement) {
1401         if (replacement == null)
1402             throw new NullPointerException("replacement");
1403         reset();
1404         if (!find())
1405             return text.toString();
1406         StringBuilder sb = new StringBuilder();
1407         appendReplacement(sb, replacement);
1408         appendTail(sb);

1409         return sb.toString();
1410     }
1411 
1412     /**
1413      * Replaces the first subsequence of the input sequence that matches the
1414      * pattern with the result of applying the given replacer function to the
1415      * match result of this matcher corresponding to that subsequence.
1416      * Exceptions thrown by the replace function are relayed to the caller.
1417      *
1418      * <p> This method first resets this matcher.  It then scans the input
1419      * sequence looking for a match of the pattern.  Characters that are not
1420      * part of the match are appended directly to the result string; the match
1421      * is replaced in the result by the applying the replacer function that
1422      * returns a replacement string.  The replacement string may contain
1423      * references to captured subsequences as in the {@link #appendReplacement
1424      * appendReplacement} method.
1425      *
1426      * <p>Note that backslashes ({@code \}) and dollar signs ({@code $}) in
1427      * the replacement string may cause the results to be different than if it
1428      * were being treated as a literal replacement string. Dollar signs may be




 976      * @param  sb
 977      *         The target string builder
 978      * @param  replacement
 979      *         The replacement string
 980      * @return  This matcher
 981      *
 982      * @throws  IllegalStateException
 983      *          If no match has yet been attempted,
 984      *          or if the previous match operation failed
 985      * @throws  IllegalArgumentException
 986      *          If the replacement string refers to a named-capturing
 987      *          group that does not exist in the pattern
 988      * @throws  IndexOutOfBoundsException
 989      *          If the replacement string refers to a capturing group
 990      *          that does not exist in the pattern
 991      * @since 9
 992      */
 993     public Matcher appendReplacement(StringBuilder sb, String replacement) {
 994         // If no match, return error
 995         if (first < 0)
 996 
 997             throw new IllegalStateException("No match available");
 998         StringBuilder result = new StringBuilder();
 999         appendExpandedReplacement(replacement, result);
1000         // Append the intervening text
1001         sb.append(text, lastAppendPosition, first);
1002         // Append the match substitution
1003         sb.append(result);
1004         lastAppendPosition = last;
1005         modCount++;
1006         return this;
1007     }
1008 
1009     /**
1010      * Processes replacement string to replace group references with
1011      * groups.
1012      */
1013     private StringBuilder appendExpandedReplacement(
1014         String replacement, StringBuilder result) {
1015         int cursor = 0;
1016         while (cursor < replacement.length()) {


1162      * {@code "-"}, an invocation of this method on a matcher for that
1163      * expression would yield the string {@code "-foo-foo-foo-"}.
1164      *
1165      * <p> Invoking this method changes this matcher's state.  If the matcher
1166      * is to be used in further matching operations then it should first be
1167      * reset.  </p>
1168      *
1169      * @param  replacement
1170      *         The replacement string
1171      *
1172      * @return  The string constructed by replacing each matching subsequence
1173      *          by the replacement string, substituting captured subsequences
1174      *          as needed
1175      */
1176     public String replaceAll(String replacement) {
1177         reset();
1178         boolean result = find();
1179         if (result) {
1180             StringBuilder sb = new StringBuilder();
1181             do {
1182                 sb.append(text, lastAppendPosition, first);
1183                 appendExpandedReplacement(replacement, sb);
1184                 lastAppendPosition = last;
1185                 result = find();
1186             } while (result);
1187             sb.append(text, lastAppendPosition, getTextLength());
1188             return sb.toString();
1189         }
1190         return text.toString();
1191     }
1192 
1193     /**
1194      * Replaces every subsequence of the input sequence that matches the
1195      * pattern with the result of applying the given replacer function to the
1196      * match result of this matcher corresponding to that subsequence.
1197      * Exceptions thrown by the function are relayed to the caller.
1198      *
1199      * <p> This method first resets this matcher.  It then scans the input
1200      * sequence looking for matches of the pattern.  Characters that are not
1201      * part of any match are appended directly to the result string; each match
1202      * is replaced in the result by the applying the replacer function that
1203      * returns a replacement string.  Each replacement string may contain
1204      * references to captured subsequences as in the {@link #appendReplacement
1205      * appendReplacement} method.
1206      *
1207      * <p> Note that backslashes ({@code \}) and dollar signs ({@code $}) in


1390      * {@code "cat"}, an invocation of this method on a matcher for that
1391      * expression would yield the string {@code "zzzcatzzzdogzzz"}.  </p>
1392      *
1393      * <p> Invoking this method changes this matcher's state.  If the matcher
1394      * is to be used in further matching operations then it should first be
1395      * reset.  </p>
1396      *
1397      * @param  replacement
1398      *         The replacement string
1399      * @return  The string constructed by replacing the first matching
1400      *          subsequence by the replacement string, substituting captured
1401      *          subsequences as needed
1402      */
1403     public String replaceFirst(String replacement) {
1404         if (replacement == null)
1405             throw new NullPointerException("replacement");
1406         reset();
1407         if (!find())
1408             return text.toString();
1409         StringBuilder sb = new StringBuilder();
1410         sb.append(text, 0, first);
1411         appendExpandedReplacement(replacement, sb);
1412         sb.append(text, last, getTextLength());
1413         return sb.toString();
1414     }
1415 
1416     /**
1417      * Replaces the first subsequence of the input sequence that matches the
1418      * pattern with the result of applying the given replacer function to the
1419      * match result of this matcher corresponding to that subsequence.
1420      * Exceptions thrown by the replace function are relayed to the caller.
1421      *
1422      * <p> This method first resets this matcher.  It then scans the input
1423      * sequence looking for a match of the pattern.  Characters that are not
1424      * part of the match are appended directly to the result string; the match
1425      * is replaced in the result by the applying the replacer function that
1426      * returns a replacement string.  The replacement string may contain
1427      * references to captured subsequences as in the {@link #appendReplacement
1428      * appendReplacement} method.
1429      *
1430      * <p>Note that backslashes ({@code \}) and dollar signs ({@code $}) in
1431      * the replacement string may cause the results to be different than if it
1432      * were being treated as a literal replacement string. Dollar signs may be