⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 re.java

📁 jakarta-regexp-1.5 正则表达式的源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        // 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     */    public boolean match(String search, int i)    {        return match(new StringCharacterIterator(search), i);    }    /**     * 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     */    public boolean match(CharacterIterator search, int i)    {        // There is no compiled program to search with!        if (program == null)        {            // 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        this.search = search;        // Can we optimize the search by looking for new lines?        if ((program.flags & REProgram.OPT_HASBOL) == REProgram.OPT_HASBOL)        {            // Non multi-line matching with BOL: Must match at '0' index            if ((matchFlags & MATCH_MULTILINE) == 0)            {                return i == 0 && matchAt(i);            }            // Multi-line matching with BOL: Seek to next line            for ( ;! search.isEnd(i); i++)            {                // Skip if we are at the beginning of the line                if (isNewline(i))                {                    continue;                }                // Match at the beginning of the line                if (matchAt(i))                {                    return true;                }                // Skip to the end of line                for ( ;! search.isEnd(i); i++)                {                    if (isNewline(i))                    {                        break;                    }                }            }            return false;        }        // Can we optimize the search by looking for a prefix string?        if (program.prefix == null)        {            // Unprefixed matching must try for a match at each character            for ( ;! search.isEnd(i - 1); i++)            {                // Try a match at index i                if (matchAt(i))                {                    return true;                }            }            return false;        }        else        {            // Prefix-anchored matching is possible            boolean caseIndependent = (matchFlags & MATCH_CASEINDEPENDENT) != 0;            char[] prefix = program.prefix;            for ( ; !search.isEnd(i + prefix.length - 1); i++)            {                int j = i;                int k = 0;                boolean match;                do {                    // If there's a mismatch of any character in the prefix, give up                    match = (compareChars(search.charAt(j++), prefix[k++], caseIndependent) == 0);                } while (match && k < prefix.length);                // See if the whole prefix string matched                if (k == prefix.length)                {                    // 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     */    public boolean match(String search)    {        return match(search, 0);    }    /**     * 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     */    public String[] split(String s)    {        // Create new vector        Vector v = new Vector();        // Start at position 0 and search the whole string        int pos = 0;        int len = s.length();        // Try a match at each position        while (pos < len && match(s, pos))        {            // Get start of match            int start = getParenStart(0);            // Get end of match            int newpos = getParenEnd(0);            // Check if no progress was made            if (newpos == pos)            {                v.addElement(s.substring(pos, start + 1));                newpos++;            }            else            {                v.addElement(s.substring(pos, start));            }            // Move to new position            pos = newpos;        }        // Push remainder if it's not empty        String remainder = s.substring(pos);        if (remainder.length() != 0)        {            v.addElement(remainder);        }        // Return vector as an array of strings        String[] ret = new String[v.size()];        v.copyInto(ret);        return ret;    }    /**     * Flag bit that indicates that subst should replace all occurrences of this     * regular expression.     */    public static final int REPLACE_ALL            = 0x0000;    /**     * Flag bit that indicates that subst should only replace the first occurrence     * of this regular expression.     */    public static final int REPLACE_FIRSTONLY      = 0x0001;    /**     * Flag bit that indicates that subst should replace backreferences     */    public static final int REPLACE_BACKREFERENCES = 0x0002;    /**     * 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).     */    public String subst(String substituteIn, String substitution)    {        return subst(substituteIn, substitution, REPLACE_ALL);    }    /**     * 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 "&lt;a href=\"$0\"&gt;$0&lt;/a&gt;", the resulting String     * returned by subst would be     * "visit us: &lt;a href=\"http://www.apache.org\"&gt;http://www.apache.org&lt;/a&gt;!".     * <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).     */    public String subst(String substituteIn, String substitution, int flags)    {        // String to return        StringBuffer ret = new StringBuffer();        // Start at position 0 and search the whole string        int pos = 0;        int len = substituteIn.length();        // Try a match at each position        while (pos < len && match(substituteIn, pos))        {            // Append string before match            ret.append(substituteIn.substring(pos, getParenStart(0)));            if ((flags & REPLACE_BACKREFERENCES) != 0)            {                // Process backreferences                int lCurrentPosition = 0;                int lLastPosition = -2;                int lLength = substitution.length();                while ((lCurrentPosition = substitution.indexOf("$", lCurrentPosition)) >= 0)                {                    if ((lCurrentPosition == 0 || substitution.charAt(lCurrentPosition - 1) != '\\')                        && lCurrentPosition + 1 < lLength)                    {                        char c = substitution.charAt(lCurrentPosition + 1);                        if (c >= '0' && c <= '9')                        {                            // Append everything between the last and the current $ sign                            ret.append(substitution.substring(lLastPosition + 2, lCurrentPosition));                            // Append the parenthesized expression, if present                            String val = getParen(c - '0');                            if (val != null) {                                ret.append(val);                            }                            lLastPosition = lCurrentPosition;                        }                    }                    // Move forward, skipping past match                    lCurrentPosition++;                }                // Append everything after the last $ sign                ret.append(substitution.substring(lLastPosition + 2, lLength));            }            else            {                // Append substitution without processing backreferences                ret.append(substitution);            }            // Move forward, skipping past match            int newpos = getParenEnd(0);            // We always want to make progress!            if (newpos == pos)            {                newpos++;            }            // Try new position            pos = newpos;            // Break out if we're only supposed to replace one occurrence            if ((flags & REPLACE_FIRSTONLY) != 0)            {                break;            }        }        // If there's remaining input, append it        if (pos < len)        {            ret.append(substituteIn.substring(pos));        }        // Return string buffer as string        return ret.toString();    }    /**     * 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 String

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -