-
Enhancement
-
Resolution: Fixed
-
P4
-
1.4.0
-
beta2
-
generic
-
generic
-
Verified
Name: bsC130419 Date: 06/25/2001
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
I'm writing a syntax-highlighting editor application that
relies heavily on regular expressions, in particular for the
search-and-replace dialog and the tokenizer. In both areas,
I need to be able to specify the index at which the find()
method should start looking, but Java's new regex package
doesn't provide that capability. In Perl, I could do this by
setting the "pos" variable; in the Jakarta-ORO package, I
would use PatternMatcherInput.setCurrentOffset().
Currently, if you want to specify that a find() should begin
at a given position within some text, you have to create a new
CharSequence from the text, starting at the desired position,
and create a new matcher from the CharSequence. Once you've
found a match, if you want to, e.g., highlight the result in
a text component, you have to convert the start and end indexes
of the match by adding the original starting index to them.
And, as inefficient, arduous, and error-prone as this process
is, it still doesn't work right--since the matcher thinks it's
at the beginning of the input, word boundaries (as the first
atom in the regex) and lookbehinds won't match correctly.
I copied the source code of the java.util.regex package into
my app, for testing purposes, and added the following method
to the Matcher class--works fine.
--------------------- recommended addition ------------------------
/**
* Attempts to find the next subsequence of the input sequence
* that matches the pattern, starting at the given index.
*
* <p> If the match succeeds then more information can be obtained
* via the <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.
* </p>
*
* @param startingAt
* The index at which the matcher should start looking
*
* @return <tt>true</tt> if, and only if, a subsequence of the
* inputsequence matches this matcher's pattern
*
* @throws IndexOutOfBoundsException
* If the index is not valid for the input sequence
*/
public boolean find(int startingAt) {
if (startingAt < 0 || startingAt > getTextLength())
throw new IndexOutOfBoundsException("Bad start position: "
+ startingAt);
return find(startingAt, getTextLength());
}
(Review ID: 127239)
======================================================================