/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* class. Regular expressions are pattern descriptions which enable
* sophisticated matching of strings. In addition to being able to
* match a string against a pattern, you can also extract parts of the
* match. This is especially useful in text parsing! Details on the
* syntax of regular expression patterns are given below.
*
* <p>
* To compile a regular expression (RE), you can simply construct an RE
* matcher object from the string specification of the pattern, like this:
*
* <pre>
* RE r = new RE("a*b");
* </pre>
*
* <p>
* Once you have done this, you can call either of the RE.match methods to
* perform matching on a String. For example:
*
* <pre>
* boolean matched = r.match("aaaab");
* </pre>
*
* will cause the boolean matched to be set to true because the
* pattern "a*b" matches the string "aaaab".
*
* <p>
* If you were interested in the <i>number</i> of a's which matched the
* first part of our example expression, you could change the expression to
* "(a*)b". Then when you compiled the expression and matched it against
* something like "xaaaab", you would get results like this:
*
* <pre>
* RE r = new RE("(a*)b"); // Compile expression
* boolean matched = r.match("xaaaab"); // Match against "xaaaab"
*
* String wholeExpr = r.getParen(0); // wholeExpr will be 'aaaab'
* String insideParens = r.getParen(1); // insideParens will be 'aaaa'
*
* int startWholeExpr = r.getParenStart(0); // startWholeExpr will be index 1
* int endWholeExpr = r.getParenEnd(0); // endWholeExpr will be index 6
* int lenWholeExpr = r.getParenLength(0); // lenWholeExpr will be 5
*
* int startInside = r.getParenStart(1); // startInside will be index 1
* int endInside = r.getParenEnd(1); // endInside will be index 5
* int lenInside = r.getParenLength(1); // lenInside will be 4
* </pre>
*
* You can also refer to the contents of a parenthesized expression
* within a regular expression itself. This is called a
* 'backreference'. The first backreference in a regular expression is
* denoted by \1, the second by \2 and so on. So the expression:
*
* <pre>
* ([0-9]+)=\1
* </pre>
*
* will match any string of the form n=n (like 0=0 or 2=2).
*
* <p>
* The full regular expression syntax accepted by RE is described here:
*
* <pre>
*
* <b><font face=times roman>Characters</font></b>
*
* <i>unicodeChar</i> Matches any identical unicode character
* \ Used to quote a meta-character (like '*')
* \\ Matches a single '\' character
* \0nnn Matches a given octal character
* \xhh Matches a given 8-bit hexadecimal character
* \\uhhhh Matches a given 16-bit hexadecimal character
* \t Matches an ASCII tab character
* \n Matches an ASCII newline character
* \r Matches an ASCII return character
* \f Matches an ASCII form feed character
*
*
* <b><font face=times roman>Character Classes</font></b>
*
* [abc] Simple character class
* [a-zA-Z] Character class with ranges
* [^abc] Negated character class
* </pre>
*
* <b>NOTE:</b> Incomplete ranges will be interpreted as "starts
* from zero" or "ends with last character".
* <br>
* I.e. [-a] is the same as [\\u0000-a], and [a-] is the same as [a-\\uFFFF],
* [-] means "all characters".
*
* <pre>
*
* <b><font face=times roman>Standard POSIX Character Classes</font></b>
*
* [:alnum:] Alphanumeric characters.
* [:alpha:] Alphabetic characters.
* [:blank:] Space and tab characters.
* [:cntrl:] Control characters.
* [:digit:] Numeric characters.
* [:graph:] Characters that are printable and are also visible.
* (A space is printable, but not visible, while an
* `a' is both.)
* [:lower:] Lower-case alphabetic characters.
* [:print:] Printable characters (characters that are not
* control characters.)
* [:punct:] Punctuation characters (characters that are not letter,
* digits, control characters, or space characters).
* [:space:] Space characters (such as space, tab, and formfeed,
* to name a few).
* [:upper:] Upper-case alphabetic characters.
* [:xdigit:] Characters that are hexadecimal digits.
*
*
* <b><font face=times roman>Non-standard POSIX-style Character Classes</font></b>
*
* [:javastart:] Start of a Java identifier
* [:javapart:] Part of a Java identifier
*
*
* <b><font face=times roman>Predefined Classes</font></b>
*
* . Matches any character other than newline
* \w Matches a "word" character (alphanumeric plus "_")
* \W Matches a non-word character
* \s Matches a whitespace character
* \S Matches a non-whitespace character
* \d Matches a digit character
* \D Matches a non-digit character
*
*
* <b><font face=times roman>Boundary Matchers</font></b>
*
* ^ Matches only at the beginning of a line
* $ Matches only at the end of a line
* \b Matches only at a word boundary
* \B Matches only at a non-word boundary
*
*
* <b><font face=times roman>Greedy Closures</font></b>
*
* A* Matches A 0 or more times (greedy)
* A+ Matches A 1 or more times (greedy)
* A? Matches A 1 or 0 times (greedy)
* A{n} Matches A exactly n times (greedy)
* A{n,} Matches A at least n times (greedy)
* A{n,m} Matches A at least n but not more than m times (greedy)
*
*
* <b><font face=times roman>Reluctant Closures</font></b>
*
* A*? Matches A 0 or more times (reluctant)
* A+? Matches A 1 or more times (reluctant)
* A?? Matches A 0 or 1 times (reluctant)
*
*
* <b><font face=times roman>Logical Operators</font></b>
*
* AB Matches A followed by B
* A|B Matches either A or B
* (A) Used for subexpression grouping
* (?:A) Used for subexpression clustering (just like grouping but
* no backrefs)
*
*
* <b><font face=times roman>Backreferences</font></b>
*
* \1 Backreference to 1st parenthesized subexpression
* \2 Backreference to 2nd parenthesized subexpression
* \3 Backreference to 3rd parenthesized subexpression
* \4 Backreference to 4th parenthesized subexpression
* \5 Backreference to 5th parenthesized subexpression
* \6 Backreference to 6th parenthesized subexpression
* \7 Backreference to 7th parenthesized subexpression
* \8 Backreference to 8th parenthesized subexpression
* \9 Backreference to 9th parenthesized subexpression
* </pre>
*
* <p>
* All closure operators (+, *, ?, {m,n}) are greedy by default, meaning
* that they match as many elements of the string as possible without
* causing the overall match to fail. If you want a closure to be
* reluctant (non-greedy), you can simply follow it with a '?'. A
* reluctant closure will match as few elements of the string as
* possible when finding matches. {m,n} closures don't currently
* support reluctancy.
*
* <p>
* <b><font face="times roman">Line terminators</font></b>
* <br>
* A line terminator is a one- or two-character sequence that marks
* the end of a line of the input character sequence. The following
* are recognized as line terminators:
* <ul>
* <li>A newline (line feed) character ('\n'),</li>
* <li>A carriage-return character followed immediately by a newline character ("\r\n"),</li>
* <li>A standalone carriage-return character ('\r'),</li>
* <li>A next-line character ('\u0085'),</li>
* <li>A line-separator character ('\u2028'), or</li>
* <li>A paragraph-separator character ('\u2029).</li>
* </ul>
*
* <p>
* RE runs programs compiled by the RECompiler class. But the RE
* matcher class does not include the actual regular expression compiler
* for reasons of efficiency. In fact, if you want to pre-compile one
* or more regular expressions, the 'recompile' class can be invoked
* from the command line to produce compiled output like this:
*
* <pre>
* // Pre-compiled regular expression "a*b"
* char[] re1Instructions =
* {
* 0x007c, 0x0000, 0x001a, 0x007c, 0x0000, 0x000d, 0x0041,
* 0x0001, 0x0004, 0x0061, 0x007c, 0x0000, 0x0003, 0x0047,
* 0x0000, 0xfff6, 0x007c, 0x0000, 0x0003, 0x004e, 0x0000,
* 0x0003, 0x0041, 0x0001, 0x0004, 0x0062, 0x0045, 0x0000,
* 0x0000,
* };
*
*
* REProgram re1 = new REProgram(re1Instructions);
* </pre>
*
* You can then construct a regular expression matcher (RE) object from
* the pre-compiled expression re1 and thus avoid the overhead of
* compiling the expression at runtime. If you require more dynamic
* regular expressions, you can construct a single RECompiler object and
* re-use it to compile each expression. Similarly, you can change the
* program run by a given matcher object at any time. However, RE and
* RECompiler are not threadsafe (for efficiency reasons, and because
* requiring thread safety in this class is deemed to be a rare
* requirement), so you will need to construct a separate compiler or
* matcher object for each thread (unless you do thread synchronization
* yourself). Once expression compiled into the REProgram object, REProgram
* can be safely shared across multiple threads and RE objects.
*
* <br><p><br>
*
* <font color="red">
* <i>ISSUES:</i>
*
* <ul>
* <li>com.weusours.util.re is not currently compatible with all
* standard POSIX regcomp flags</li>
* <li>com.weusours.util.re does not support POSIX equivalence classes
* <li>com.weusours.util.re does not support nested POSIX character
* classes (definitely should, but not completely trivial)</li>
* <li>com.weusours.util.re Does not support POSIX character collation
* <li>Should there be different matching styles (simple, POSIX, Perl etc?)</li>
* <li>Should RE support character iterators (for backwards RE matching!)?</li>
* <li>Should RE support reluctant {m,n} closures (does anyone care)?</li>
* <li>Not *all* possibilities are considered for greediness when backreferences
* are involved (as POSIX suggests should be the case). The POSIX RE
* "(ac*)c*d[ac]*\1", when matched against "acdacaa" should yield a match
* of acdacaa where \1 is "a". This is not the case in this RE package,
* and actually Perl doesn't go to this extent either! Until someone
* actually complains about this, I'm not sure it's worth "fixing".
* If it ever is fixed, test #137 in RETest.txt should be updated.</li>
* </ul>
*
* </font>
*
* @see recompile
* @see RECompiler
*
* @author <a href="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
* @author <a href="mailto:ts@sch-fer.de">Tobias Schäfer</a>
*/
{
/**
* Specifies normal, case-sensitive matching behaviour.
*/
/**
* Flag to indicate that matching should be case-independent (folded)
*/
/**
*/
/**
* Consider all input a single body of text - newlines are matched by .
*/
/************************************************
* *
* The format of a node in a program is: *
* *
* [ OPCODE ] [ OPDATA ] [ OPNEXT ] [ OPERAND ] *
* *
* char OPCODE - instruction *
* char OPDATA - modifying data *
* char OPNEXT - next node (relative offset) *
* *
************************************************/
// ---------- ---------- --------------- --------------------------------------------------
static final char OP_RELUCTANTSTAR = '8'; // none/expr reluctant '*' (mnemonic for char is unshifted '*')
static final char OP_RELUCTANTPLUS = '='; // none/expr reluctant '+' (mnemonic for char is unshifted '+')
static final char OP_RELUCTANTMAYBE = '/'; // none/expr reluctant '?' (mnemonic for char is unshifted '?')
// Escape codes
// Posix character classes
// Limits
// Node layout constants
// State of current program
// Parenthesized subexpressions
// Backreferences
/**
* Constructs a regular expression matcher from a String by compiling it
* using a new instance of RECompiler. If you will be compiling many
* expressions, you may prefer to use a single RECompiler object instead.
*
* @param pattern The regular expression pattern to compile.
* @exception RESyntaxException Thrown if the regular expression has invalid syntax.
* @see RECompiler
* @see recompile
*/
{
this(pattern, MATCH_NORMAL);
}
/**
* Constructs a regular expression matcher from a String by compiling it
* using a new instance of RECompiler. If you will be compiling many
* expressions, you may prefer to use a single RECompiler object instead.
*
* @param pattern The regular expression pattern to compile.
* @param matchFlags The matching style
* @exception RESyntaxException Thrown if the regular expression has invalid syntax.
* @see RECompiler
* @see recompile
*/
{
}
/**
* Construct a matcher for a pre-compiled regular expression from program
* (bytecode) data. Permits special flags to be passed in to modify matching
* behaviour.
*
* @param matchFlags One or more of the RE match behaviour flags (RE.MATCH_*):
*
* <pre>
* MATCH_NORMAL // Normal (case-sensitive) matching
* MATCH_CASEINDEPENDENT // Case folded comparisons
* </pre>
*
* @see RECompiler
* @see REProgram
* @see recompile
*/
{
}
/**
* Construct a matcher for a pre-compiled regular expression from program
* (bytecode) data.
*
* @param program Compiled regular expression program
* @see RECompiler
* @see recompile
*/
{
this(program, MATCH_NORMAL);
}
/**
* Constructs a regular expression matcher with no initial program.
* This is likely to be an uncommon practice, but is still supported.
*/
public RE()
{
}
/**
* Converts a 'simplified' regular expression to a full regular expression
*
* @param pattern The pattern to convert
* @return The full regular expression
*/
{
{
switch (c)
{
case '*':
break;
case '.':
case '[':
case ']':
case '\\':
case '+':
case '?':
case '{':
case '}':
case '$':
case '^':
case '|':
case '(':
case ')':
default:
break;
}
}
}
/**
* Sets match behaviour flags which alter the way RE does matching.
* @param matchFlags One or more of the RE match behaviour flags (RE.MATCH_*):
*
* <pre>
* MATCH_NORMAL // Normal (case-sensitive) matching
* MATCH_CASEINDEPENDENT // Case folded comparisons
* </pre>
*/
{
this.matchFlags = matchFlags;
}
/**
* Returns the current match behaviour flags.
* @return Current match behaviour flags (RE.MATCH_*).
*
* <pre>
* MATCH_NORMAL // Normal (case-sensitive) matching
* MATCH_CASEINDEPENDENT // Case folded comparisons
* </pre>
*
* @see #setMatchFlags
*/
public int getMatchFlags()
{
return matchFlags;
}
/**
* Sets the current regular expression program used by this matcher object.
*
* @param program Regular expression program compiled by RECompiler.
* @see RECompiler
* @see REProgram
* @see recompile
*/
{
} else {
}
}
/**
* Returns the current regular expression program in use by this matcher object.
*
* @return Regular expression program
* @see #setProgram
*/
{
return program;
}
/**
* Returns the number of parenthesized subexpressions available after a successful match.
*
* @return Number of available parenthesized subexpressions
*/
public int getParenCount()
{
return parenCount;
}
/**
* Gets the contents of a parenthesized subexpression after a successful match.
*
* @param which Nesting level of subexpression
* @return String
*/
{
int start;
{
}
return null;
}
/**
* Returns the start index of a given paren level.
*
* @param which Nesting level of subexpression
* @return String index
*/
{
if (which < parenCount)
{
switch (which)
{
case 0:
return start0;
case 1:
return start1;
case 2:
return start2;
default:
{
allocParens();
}
}
}
return -1;
}
/**
* Returns the end index of a given paren level.
*
* @param which Nesting level of subexpression
* @return String index
*/
{
if (which < parenCount)
{
switch (which)
{
case 0:
return end0;
case 1:
return end1;
case 2:
return end2;
default:
{
allocParens();
}
}
}
return -1;
}
/**
* Returns the length of a given paren level.
*
* @param which Nesting level of subexpression
* @return Number of characters in the parenthesized subexpression
*/
{
if (which < parenCount)
{
}
return -1;
}
/**
* Sets the start of a paren level
*
* @param which Which paren level
* @param i Index in input array
*/
{
if (which < parenCount)
{
switch (which)
{
case 0:
start0 = i;
break;
case 1:
start1 = i;
break;
case 2:
start2 = i;
break;
default:
{
allocParens();
}
break;
}
}
}
/**
* Sets the end of a paren level
*
* @param which Which paren level
* @param i Index in input array
*/
{
if (which < parenCount)
{
switch (which)
{
case 0:
end0 = i;
break;
case 1:
end1 = i;
break;
case 2:
end2 = i;
break;
default:
{
allocParens();
}
break;
}
}
}
/**
* Throws an Error representing an internal error condition probably resulting
* from a bug in the regular expression compiler (or possibly data corruption).
* In practice, this should be very rare.
*
* @param s Error description
*/
{
throw new Error("RE internal error: " + s);
}
/**
* Performs lazy allocation of subexpression arrays
*/
private final void allocParens()
{
// Allocate arrays for subexpressions
// Set sub-expression pointers to invalid values
for (int i = 0; i < maxParen; i++)
{
startn[i] = -1;
endn[i] = -1;
}
}
/**
* Try to match a string against a subset of nodes in the program
*
* @param firstNode Node to start at in program
* @param lastNode Last valid node (used for matching a subexpression without
* matching the rest of the program as well).
* @param idxStart Starting position in character array
* @return Final input array index if match succeeded. -1 if not.
*/
{
// Our current place in the string
// Loop while node is valid
int idxNew;
{
switch (opcode)
{
case OP_RELUCTANTMAYBE:
{
int once = 0;
do
{
// Try to match the rest without using the reluctant subexpr
{
return idxNew;
}
}
return -1;
}
case OP_RELUCTANTPLUS:
{
// Try to match the rest without using the reluctant subexpr
{
return idxNew;
}
}
return -1;
case OP_RELUCTANTSTAR:
do
{
// Try to match the rest without using the reluctant subexpr
{
return idxNew;
}
}
return -1;
case OP_OPEN:
// Match subexpression
{
}
{
// Increase valid paren count
{
}
// Don't set paren if already set later on
{
}
}
return idxNew;
case OP_CLOSE:
// Done matching subexpression
{
}
{
// Increase valid paren count
{
}
// Don't set paren if already set later on
{
}
}
return idxNew;
case OP_OPEN_CLUSTER:
case OP_CLOSE_CLUSTER:
// starting or ending the matching of a subexpression which has no backref.
case OP_BACKREF:
{
// Get the start and end of the backref
int s = startBackref[opdata];
int e = endBackref[opdata];
// We don't know the backref yet
if (s == -1 || e == -1)
{
return -1;
}
// The backref is empty size
if (s == e)
{
break;
}
// Get the length of the backref
int l = e - s;
// If there's not enough input left, give up.
{
return -1;
}
// Case fold the backref?
final boolean caseFold =
// Compare backref to input
for (int i = 0; i < l; i++)
{
{
return -1;
}
}
}
break;
case OP_BOL:
// Fail if we're not at the start of the string
if (idx != 0)
{
// If we're multiline matching, we could still be at the start of a line
{
// If not at start of line, give up
return -1;
} else {
break;
}
}
return -1;
}
break;
case OP_EOL:
// If we're not at the end of string
{
// If we're multi-line matching
{
// Give up if we're not at the end of a line
return -1;
} else {
break;
}
}
return -1;
}
break;
case OP_ESCAPE:
// Which escape?
switch (opdata)
{
// Word boundary match
case E_NBOUND:
case E_BOUND:
{
{
return -1;
}
}
break;
// Alpha-numeric, digit, space, javaLetter, javaLetterOrDigit
case E_ALNUM:
case E_NALNUM:
case E_DIGIT:
case E_NDIGIT:
case E_SPACE:
case E_NSPACE:
// Give up if out of input
{
return -1;
}
// Switch on escape
switch (opdata)
{
case E_ALNUM:
case E_NALNUM:
{
return -1;
}
break;
case E_DIGIT:
case E_NDIGIT:
{
return -1;
}
break;
case E_SPACE:
case E_NSPACE:
{
return -1;
}
break;
}
idx++;
break;
default:
}
break;
case OP_ANY:
// Match anything
{
return -1;
}
}
else
{
// Match anything but a newline
{
return -1;
}
}
idx++;
break;
case OP_ATOM:
{
// Match an atom value
{
return -1;
}
// Get length of atom and starting index
// Give up if not enough input remains to have a match
{
return -1;
}
// Match atom differently depending on casefolding flag
final boolean caseFold =
for (int i = 0; i < lenAtom; i++)
{
{
return -1;
}
}
}
break;
case OP_POSIXCLASS:
{
// Out of input?
{
return -1;
}
switch (opdata)
{
case POSIX_CLASS_ALNUM:
{
return -1;
}
break;
case POSIX_CLASS_ALPHA:
{
return -1;
}
break;
case POSIX_CLASS_DIGIT:
{
return -1;
}
break;
case POSIX_CLASS_BLANK: // JWL - bugbug: is this right??
{
return -1;
}
break;
case POSIX_CLASS_SPACE:
{
return -1;
}
break;
case POSIX_CLASS_CNTRL:
{
return -1;
}
break;
case POSIX_CLASS_GRAPH: // JWL - bugbug???
{
case Character.MATH_SYMBOL:
case Character.CURRENCY_SYMBOL:
case Character.MODIFIER_SYMBOL:
case Character.OTHER_SYMBOL:
break;
default:
return -1;
}
break;
case POSIX_CLASS_LOWER:
{
return -1;
}
break;
case POSIX_CLASS_UPPER:
{
return -1;
}
break;
case POSIX_CLASS_PRINT:
{
return -1;
}
break;
case POSIX_CLASS_PUNCT:
{
switch(type)
{
case Character.DASH_PUNCTUATION:
case Character.START_PUNCTUATION:
case Character.END_PUNCTUATION:
case Character.OTHER_PUNCTUATION:
break;
default:
return -1;
}
}
break;
case POSIX_CLASS_XDIGIT: // JWL - bugbug??
{
if (!isXDigit)
{
return -1;
}
}
break;
case POSIX_CLASS_JSTART:
{
return -1;
}
break;
case POSIX_CLASS_JPART:
{
return -1;
}
break;
default:
internalError("Bad posix class");
break;
}
// Matched.
idx++;
}
break;
case OP_ANYOF:
{
// Out of input?
{
return -1;
}
// Get character to match against character class and maybe casefold
// Loop through character class checking our match character
boolean match = false;
{
// Get start, end and match characters
char s = instruction[i++];
char e = instruction[i++];
}
// Fail if we didn't match the character class
if (!match)
{
return -1;
}
idx++;
}
break;
case OP_BRANCH:
{
// Check for choices
{
// If there aren't any other choices, just evaluate this branch.
continue;
}
// Try all available branches
short nextBranch;
do
{
// Try matching the branch against the string
{
return idxNew;
}
// Go to next branch (if any)
node += nextBranch;
}
// Failed to match any branch!
return -1;
}
case OP_NOTHING:
case OP_GOTO:
// Just advance to the next node without doing anything
break;
case OP_END:
// Match has succeeded!
return idx;
default:
// Corrupt program
}
// Advance to the next node in the program
}
// We "should" never end up here
internalError("Corrupt program");
return -1;
}
/**
* Match the current regular expression program against the current
* input string, starting at index i of the input string. This method
* is only meant for internal use.
*
* @param i The input string index to start matching at
* @return True if the input matched the expression
*/
protected boolean matchAt(int i)
{
// Initialize start pointer, paren cache and paren count
start0 = -1;
end0 = -1;
start1 = -1;
end1 = -1;
start2 = -1;
end2 = -1;
parenCount = 1;
setParenStart(0, i);
// Allocate backref arrays (unless optimizations indicate otherwise)
{
startBackref = new int[maxParen];
endBackref = new int[maxParen];
}
// Match against string
int idx;
{
return true;
}
// Didn't match
parenCount = 0;
return false;
}
/**
* Matches the current regular expression program against a character array,
* starting at a given index.
*
* @param search String to match against
* @param i Index to start searching at
* @return True if string matched
*/
{
}
/**
* Matches the current regular expression program against a character array,
* starting at a given index.
*
* @param search String to match against
* @param i Index to start searching at
* @return True if string matched
*/
{
// There is no compiled program to search with!
{
// This should be uncommon enough to be an error case rather
// than an exception (which would have to be handled everywhere)
internalError("No RE program to run!");
}
// Save string to search
// Can we optimize the search by looking for a prefix string?
{
// Unprefixed matching must try for a match at each character
{
// Try a match at index i
if (matchAt(i))
{
return true;
}
}
return false;
}
else
{
// Prefix-anchored matching is possible
{
int j = i;
int k = 0;
boolean match;
do {
// If there's a mismatch of any character in the prefix, give up
// See if the whole prefix string matched
{
// We matched the full prefix at firstChar, so try it
if (matchAt(i))
{
return true;
}
}
}
return false;
}
}
/**
* Matches the current regular expression program against a String.
*
* @param search String to match against
* @return True if string matched
*/
{
}
/**
* Splits a string into an array of strings on regular expression boundaries.
* This function works the same way as the Perl function of the same name.
* Given a regular expression of "[ab]+" and a string to split of
* "xyzzyababbayyzabbbab123", the result would be the array of Strings
* "[xyzzy, yyz, 123]".
*
* <p>Please note that the first string in the resulting array may be an empty
* string. This happens when the very first character of input string is
* matched by the pattern.
*
* @param s String to split on this regular exression
* @return Array of strings
*/
{
// Create new vector
// Start at position 0 and search the whole string
int pos = 0;
// Try a match at each position
{
// Get start of match
// Get end of match
// Check if no progress was made
{
newpos++;
}
else
{
}
// Move to new position
}
// Push remainder if it's not empty
{
v.addElement(remainder);
}
// Return vector as an array of strings
return ret;
}
/**
* Flag bit that indicates that subst should replace all occurrences of this
* regular expression.
*/
/**
* Flag bit that indicates that subst should only replace the first occurrence
* of this regular expression.
*/
/**
* Flag bit that indicates that subst should replace backreferences
*/
/**
* Substitutes a string for this regular expression in another string.
* This method works like the Perl function of the same name.
* Given a regular expression of "a*b", a String to substituteIn of
* "aaaabfooaaabgarplyaaabwackyb" and the substitution String "-", the
* resulting String returned by subst would be "-foo-garply-wacky-".
*
* @param substituteIn String to substitute within
* @param substitution String to substitute for all matches of this regular expression.
* @return The string substituteIn with zero or more occurrences of the current
* regular expression replaced with the substitution String (if this regular
* expression object doesn't match at any position, the original String is returned
* unchanged).
*/
{
}
/**
* Substitutes a string for this regular expression in another string.
* This method works like the Perl function of the same name.
* Given a regular expression of "a*b", a String to substituteIn of
* "aaaabfooaaabgarplyaaabwackyb" and the substitution String "-", the
* resulting String returned by subst would be "-foo-garply-wacky-".
* <p>
* It is also possible to reference the contents of a parenthesized expression
* with $0, $1, ... $9. A regular expression of "http://[\\.\\w\\-\\?/~_@&=%]+",
* a String to substituteIn of "visit us: http://www.apache.org!" and the
* substitution String "<a href=\"$0\">$0</a>", the resulting String
* returned by subst would be
* "visit us: <a href=\"http://www.apache.org\">http://www.apache.org</a>!".
* <p>
* <i>Note:</i> $0 represents the whole match.
*
* @param substituteIn String to substitute within
* @param substitution String to substitute for matches of this regular expression
* @param flags One or more bitwise flags from REPLACE_*. If the REPLACE_FIRSTONLY
* flag bit is set, only the first occurrence of this regular expression is replaced.
* If the bit is not set (REPLACE_ALL), all occurrences of this pattern will be
* replaced. If the flag REPLACE_BACKREFERENCES is set, all backreferences will
* be processed.
* @return The string substituteIn with zero or more occurrences of the current
* regular expression replaced with the substitution String (if this regular
* expression object doesn't match at any position, the original String is returned
* unchanged).
*/
{
// String to return
// Start at position 0 and search the whole string
int pos = 0;
// Try a match at each position
{
// Append string before match
{
// Process backreferences
int lCurrentPosition = 0;
int lLastPosition = -2;
boolean bAddedPrefix = false;
{
{
if (c >= '0' && c <= '9')
{
if (bAddedPrefix == false)
{
// Append everything between the beginning of the
// substitution string and the current $ sign
bAddedPrefix = true;
}
else
{
// Append everything between the last and the current $ sign
}
// Append the parenthesized expression
// Note: if a parenthesized expression of the requested
// index is not available "null" is added to the string
}
}
// Move forward, skipping past match
}
// Append everything after the last $ sign
}
else
{
// Append substitution without processing backreferences
}
// Move forward, skipping past match
// We always want to make progress!
{
newpos++;
}
// Try new position
// Break out if we're only supposed to replace one occurrence
{
break;
}
}
// If there's remaining input, append it
{
}
// Return string buffer as string
}
/**
* Returns an array of Strings, whose toString representation matches a regular
* expression. This method works like the Perl function of the same name. Given
* a regular expression of "a*b" and an array of String objects of [foo, aab, zzz,
* aaaab], the array of Strings returned by grep would be [aab, aaaab].
*
* @param search Array of Objects to search
* @return Array of Strings whose toString() value matches this regular expression.
*/
{
// Create new vector to hold return items
// Traverse array of objects
{
// Get next object as a string
// If it matches this regexp, add it to the list
if (match(s))
{
v.addElement(s);
}
}
// Return vector as an array of strings
return ret;
}
/**
* @return true if character at i-th position in the <code>search</code> string is a newline
*/
private boolean isNewline(int i)
{
{
return true;
}
return false;
}
/**
* Compares two characters.
*
* @param c1 first character to compare.
* @param c2 second character to compare.
* @param caseIndependent whether comparision is case insensitive or not.
* @return negative, 0, or positive integer as the first character
* less than, equal to, or greater then the second.
*/
{
if (caseIndependent)
{
}
}
}