/** * Finds the next instance of the text/regular expression specified from the caret position. If a * match is found, it is replaced with the specified replacement string. * * @param textArea The text area in which to search. * @param context What to search for and all search options. * @return Whether a match was found (and thus replaced). * @throws PatternSyntaxException If this is a regular expression search but the search text is an * invalid regular expression. * @throws IndexOutOfBoundsException If this is a regular expression search but the replacement * text references an invalid group (less than zero or greater than the number of groups * matched). * @see #replaceAll(RTextArea, SearchContext) * @see #find(JTextArea, SearchContext) */ public static boolean replace(RTextArea textArea, SearchContext context) throws PatternSyntaxException { String toFind = context.getSearchFor(); if (toFind == null || toFind.length() == 0) { return false; } textArea.beginAtomicEdit(); try { // Regular expression replacements have their own method. if (context.isRegularExpression()) { return regexReplace(textArea, context); } // Plain text search. If we find it, replace it! // First make the dot and mark equal (get rid of any selection), as // a common use-case is the user will use "Find" to select the text // to replace, then click "Replace" to replace the current // selection. Since our find() method searches from an endpoint of // the selection, we must remove the selection to work properly. makeMarkAndDotEqual(textArea, context.getSearchForward()); if (find(textArea, context)) { textArea.replaceSelection(context.getReplaceWith()); return true; } } finally { textArea.endAtomicEdit(); } return false; }
/** * Finds the next instance of the text/regular expression specified from the caret position. If a * match is found, it is replaced with the specified replacement string. * * @param textArea The text area in which to search. * @param toFind The text/regular expression to search for. * @param replaceWith The string to replace the found text with. * @param forward Whether to search forward from the caret position or backward from it. * @param matchCase Whether the search should be case-sensitive. * @param wholeWord Whether there should be spaces or tabs on either side of the match. * @param regex Whether or not this is a regular expression search. * @return Whether a match was found (and thus replaced). * @throws PatternSyntaxException If <code>regex</code> is <code>true</code> but <code>toFind * </code> is not a valid regular expression. * @throws IndexOutOfBoundsException If <code>regex</code> is <code>true</code> and <code> * replaceWith</code> references an invalid group (less than zero or greater than the number * of groups matched). * @see #regexReplace * @see #find */ public static boolean replace( RTextArea textArea, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord, boolean regex) throws PatternSyntaxException { textArea.beginAtomicEdit(); try { // Regular expression replacements have their own method. if (regex) { return regexReplace(textArea, toFind, replaceWith, forward, matchCase, wholeWord); } // Plain text search. If we find it, replace it! // First make the dot and mark equal (get rid of any selection), as // a common use-case is the user will use "Find" to select the text // to replace, then click "Replace" to replace the current // selection. Since our find() method searches from an endpoint of // the selection, we must remove the selection to work properly. makeMarkAndDotEqual(textArea, forward); if (find(textArea, toFind, forward, matchCase, wholeWord, false)) { textArea.replaceSelection(replaceWith); return true; } } finally { textArea.endAtomicEdit(); } return false; }