Class SearchEngine
java.lang.Object
org.fife.ui.rtextarea.SearchEngine
A singleton class that can perform advanced find/replace operations
in an
RTextArea. Simply create a SearchContext and call
one of the following methods:
- Version:
- 1.0
- Author:
- Robert Futrell
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic SearchResultfind(JTextArea textArea, SearchContext context) Finds the next instance of the string/regular expression specified from the caret position.static intgetNextMatchPos(String searchFor, String searchIn, boolean forward, boolean matchCase, boolean wholeWord) SearchessearchInfor an occurrence ofsearchForeither forwards or backwards, matching case or not.static StringgetReplacementText(Matcher m, CharSequence template) Called internally bygetMatches().static SearchResultmarkAll(RTextArea textArea, SearchContext context) Marks all instances of the specified text in this text area.static SearchResultreplace(RTextArea textArea, SearchContext context) Finds the next instance of the text/regular expression specified from the caret position.static SearchResultreplaceAll(RTextArea textArea, SearchContext context) Replaces all instances of the text/regular expression specified in the specified document with the specified replacement.
-
Method Details
-
find
Finds the next instance of the string/regular expression specified from the caret position. If a match is found, it is selected in this text area.- Parameters:
textArea- The text area in which to search.context- What to search for and all search options.- Returns:
- The result of the operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the search text is an invalid regular expression.- See Also:
-
getNextMatchPos
public static int getNextMatchPos(String searchFor, String searchIn, boolean forward, boolean matchCase, boolean wholeWord) SearchessearchInfor an occurrence ofsearchForeither forwards or backwards, matching case or not.Most clients will have no need to call this method directly.
- Parameters:
searchFor- The string to look for.searchIn- The string to search in.forward- Whether to search forward or backward insearchIn.matchCase- Iftrue, do a case-sensitive search forsearchFor.wholeWord- Iftrue,searchForoccurrences embedded in longer words insearchIndon't count as matches.- Returns:
- The starting position of a match, or
-1if no match was found.
-
getReplacementText
Called internally bygetMatches(). This method assumes that the specified matcher has just found a match, and that you want to get the string with which to replace that match.Escapes simply insert the escaped character, except for
\nand\t, which insert a newline and tab respectively. Substrings of the form$\d+are considered to be matched groups. To include a literal dollar sign in your template, escape it (i.e.\$).Most clients will have no need to call this method directly.
- Parameters:
m- The matcher.template- The template for the replacement string. For example, "foo" would yield the replacement string "foo", while "$1 is the greatest" would yield different values depending on the value of the first captured group in the match.- Returns:
- The string to replace the match with.
- Throws:
IndexOutOfBoundsException- Iftemplatereferences an invalid group (less than zero or greater than the number of groups matched).
-
markAll
Marks all instances of the specified text in this text area. This method is typically only called directly in response to search events of typeSearchEvent.Type.MARK_ALL. "Mark all" behavior is automatically performed whenfind(JTextArea, SearchContext)orreplace(RTextArea, SearchContext)is called.- Parameters:
textArea- The text area in which to mark occurrences.context- The search context specifying the text to search for. It is assumed thatcontext.getMarkAll()has already been checked and returnstrue.- Returns:
- The results of the operation.
-
replace
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.- Parameters:
textArea- The text area in which to search.context- What to search for and all search options.- Returns:
- The result of the operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the search text is an invalid regular expression.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 Also:
-
replaceAll
Replaces all instances of the text/regular expression specified in the specified document with the specified replacement.- Parameters:
textArea- The text area in which to search.context- What to search for and all search options.- Returns:
- The result of the operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the replacement text is an invalid regular expression.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 Also:
-