public class SearchEngine
extends java.lang.Object
RTextArea
. Simply create a SearchContext
and call
one of the following methods:
SearchContext
限定符和类型 | 方法和说明 |
---|---|
static SearchResult |
find(javax.swing.JTextArea textArea,
SearchContext context)
Finds the next instance of the string/regular expression specified
from the caret position.
|
static int |
getNextMatchPos(java.lang.String searchFor,
java.lang.String searchIn,
boolean forward,
boolean matchCase,
boolean wholeWord)
Searches
searchIn for an occurrence of
searchFor either forwards or backwards, matching
case or not. |
static java.lang.String |
getReplacementText(java.util.regex.Matcher m,
java.lang.CharSequence template)
Called internally by
getMatches() . |
static SearchResult |
markAll(RTextArea textArea,
SearchContext context)
Marks all instances of the specified text in this text area.
|
static SearchResult |
replace(RTextArea textArea,
SearchContext context)
Finds the next instance of the text/regular expression specified from
the caret position.
|
static SearchResult |
replaceAll(RTextArea textArea,
SearchContext context)
Replaces all instances of the text/regular expression specified in
the specified document with the specified replacement.
|
public static SearchResult find(javax.swing.JTextArea textArea, SearchContext context)
textArea
- The text area in which to search.context
- What to search for and all search options.java.util.regex.PatternSyntaxException
- If this is a regular expression search
but the search text is an invalid regular expression.replace(RTextArea, SearchContext)
,
replaceAll(RTextArea, SearchContext)
public static final int getNextMatchPos(java.lang.String searchFor, java.lang.String searchIn, boolean forward, boolean matchCase, boolean wholeWord)
searchIn
for an occurrence of
searchFor
either forwards or backwards, matching
case or not.Most clients will have no need to call this method directly.
searchFor
- The string to look for.searchIn
- The string to search in.forward
- Whether to search forward or backward in
searchIn
.matchCase
- If true
, do a case-sensitive search for
searchFor
.wholeWord
- If true
, searchFor
occurrences embedded in longer words in searchIn
don't count as matches.-1
if no
match was found.public static java.lang.String getReplacementText(java.util.regex.Matcher m, java.lang.CharSequence template)
getMatches()
. 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 \n
and \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.
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.java.lang.IndexOutOfBoundsException
- If template
references
an invalid group (less than zero or greater than the number of
groups matched).public static final SearchResult markAll(RTextArea textArea, SearchContext context)
SearchEvent.Type.MARK_ALL
. "Mark all" behavior
is automatically performed when find(JTextArea, SearchContext)
or replace(RTextArea, SearchContext)
is called.textArea
- The text area in which to mark occurrences.context
- The search context specifying the text to search for.
It is assumed that context.getMarkAll()
has already
been checked and returns true
.public static SearchResult replace(RTextArea textArea, SearchContext context) throws java.util.regex.PatternSyntaxException
textArea
- The text area in which to search.context
- What to search for and all search options.java.util.regex.PatternSyntaxException
- If this is a regular expression search
but the search text is an invalid regular expression.java.lang.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).replaceAll(RTextArea, SearchContext)
,
find(JTextArea, SearchContext)
public static SearchResult replaceAll(RTextArea textArea, SearchContext context) throws java.util.regex.PatternSyntaxException
textArea
- The text area in which to search.context
- What to search for and all search options.java.util.regex.PatternSyntaxException
- If this is a regular expression search
but the replacement text is an invalid regular expression.java.lang.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).replace(RTextArea, SearchContext)
,
find(JTextArea, SearchContext)