?? re.java
字號:
} } longest.end[0] = longest.index; longest.finish(input); return longest; } } mymatch.clear(++anchor); // Append character to buffer if needed if (buffer != null && input.charAt(0) != CharIndexed.OUT_OF_BOUNDS) { buffer.append(input.charAt(0)); } } while (input.move(1)); // Special handling at end of input for e.g. "$" if (minimumLength == 0) { if (match(input, mymatch)) { mymatch.finish(input); return mymatch; } } return null; } /** * Returns an REMatchEnumeration that can be used to iterate over the * matches found in the input text. * * @param input The input text. * @return A non-null REMatchEnumeration instance. */ public REMatchEnumeration getMatchEnumeration(Object input) { return getMatchEnumeration(input,0,0); } /** * Returns an REMatchEnumeration that can be used to iterate over the * matches found in the input text. * * @param input The input text. * @param index The offset index at which the search should be begin. * @return A non-null REMatchEnumeration instance, with its input cursor * set to the index position specified. */ public REMatchEnumeration getMatchEnumeration(Object input, int index) { return getMatchEnumeration(input,index,0); } /** * Returns an REMatchEnumeration that can be used to iterate over the * matches found in the input text. * * @param input The input text. * @param index The offset index at which the search should be begin. * @param eflags The logical OR of any execution flags above. * @return A non-null REMatchEnumeration instance, with its input cursor * set to the index position specified. */ public REMatchEnumeration getMatchEnumeration(Object input, int index, int eflags) { return new REMatchEnumeration(this,makeCharIndexed(input,index),index,eflags); } /** * Substitutes the replacement text for the first match found in the input. * * @param input The input text. * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). * @return A String interpolating the substituted text. * @see REMatch#substituteInto */ public String substitute(Object input,String replace) { return substitute(input,replace,0,0); } /** * Substitutes the replacement text for the first match found in the input * beginning at the specified index position. Specifying an index * effectively causes the regular expression engine to throw away the * specified number of characters. * * @param input The input text. * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). * @param index The offset index at which the search should be begin. * @return A String containing the substring of the input, starting * at the index position, and interpolating the substituted text. * @see REMatch#substituteInto */ public String substitute(Object input,String replace,int index) { return substitute(input,replace,index,0); } /** * Substitutes the replacement text for the first match found in the input * string, beginning at the specified index position and using the * specified execution flags. * * @param input The input text. * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). * @param index The offset index at which the search should be begin. * @param eflags The logical OR of any execution flags above. * @return A String containing the substring of the input, starting * at the index position, and interpolating the substituted text. * @see REMatch#substituteInto */ public String substitute(Object input,String replace,int index,int eflags) { return substituteImpl(makeCharIndexed(input,index),replace,index,eflags); } private String substituteImpl(CharIndexed input,String replace,int index,int eflags) { StringBuffer buffer = new StringBuffer(); REMatch m = getMatchImpl(input,index,eflags,buffer); if (m==null) return buffer.toString(); buffer.append( ((eflags & REG_NO_INTERPOLATE) > 0) ? replace : m.substituteInto(replace) ); if (input.move(m.end[0])) { do { buffer.append(input.charAt(0)); } while (input.move(1)); } return buffer.toString(); } /** * Substitutes the replacement text for each non-overlapping match found * in the input text. * * @param input The input text. * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). * @return A String interpolating the substituted text. * @see REMatch#substituteInto */ public String substituteAll(Object input,String replace) { return substituteAll(input,replace,0,0); } /** * Substitutes the replacement text for each non-overlapping match found * in the input text, starting at the specified index. * * If the regular expression allows the empty string to match, it will * substitute matches at all positions except the end of the input. * * @param input The input text. * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). * @param index The offset index at which the search should be begin. * @return A String containing the substring of the input, starting * at the index position, and interpolating the substituted text. * @see REMatch#substituteInto */ public String substituteAll(Object input,String replace,int index) { return substituteAll(input,replace,index,0); } /** * Substitutes the replacement text for each non-overlapping match found * in the input text, starting at the specified index and using the * specified execution flags. * * @param input The input text. * @param replace The replacement text, which may contain $x metacharacters (see REMatch.substituteInto). * @param index The offset index at which the search should be begin. * @param eflags The logical OR of any execution flags above. * @return A String containing the substring of the input, starting * at the index position, and interpolating the substituted text. * @see REMatch#substituteInto */ public String substituteAll(Object input,String replace,int index,int eflags) { return substituteAllImpl(makeCharIndexed(input,index),replace,index,eflags); } private String substituteAllImpl(CharIndexed input,String replace,int index,int eflags) { StringBuffer buffer = new StringBuffer(); REMatch m; while ((m = getMatchImpl(input,index,eflags,buffer)) != null) { buffer.append( ((eflags & REG_NO_INTERPOLATE) > 0) ? replace : m.substituteInto(replace) ); index = m.getEndIndex(); if (m.end[0] == 0) { char ch = input.charAt(0); if (ch != CharIndexed.OUT_OF_BOUNDS) buffer.append(ch); input.move(1); } else { input.move(m.end[0]); } if (!input.isValid()) break; } return buffer.toString(); } /* Helper function for constructor */ private void addToken(REToken next) { if (next == null) return; minimumLength += next.getMinimumLength(); if (firstToken == null) { lastToken = firstToken = next; } else { // if chain returns false, it "rejected" the token due to // an optimization, and next was combined with lastToken if (lastToken.chain(next)) { lastToken = next; } } } private static REToken setRepeated(REToken current, int min, int max, int index) throws REException { if (current == null) throw new REException(getLocalizedMessage("repeat.no.token"),REException.REG_BADRPT,index); return new RETokenRepeated(current.subIndex,current,min,max); } private static int getPosixSet(char[] pattern,int index,StringBuffer buf) { // Precondition: pattern[index-1] == ':' // we will return pos of closing ']'. int i; for (i=index; i<(pattern.length-1); i++) { if ((pattern[i] == ':') && (pattern[i+1] == ']')) return i+2; buf.append(pattern[i]); } return index; // didn't match up } private int getMinMax(char[] input,int index,IntPair minMax,RESyntax syntax) throws REException { // Precondition: input[index-1] == '{', minMax != null boolean mustMatch = !syntax.get(RESyntax.RE_NO_BK_BRACES); int startIndex = index; if (index == input.length) { if (mustMatch) throw new REException(getLocalizedMessage("unmatched.brace"),REException.REG_EBRACE,index); else return startIndex; } int min,max=0; CharUnit unit = new CharUnit(); StringBuffer buf = new StringBuffer(); // Read string of digits do { index = getCharUnit(input,index,unit); if (Character.isDigit(unit.ch)) buf.append(unit.ch); } while ((index != input.length) && Character.isDigit(unit.ch)); // Check for {} tomfoolery if (buf.length() == 0) { if (mustMatch) throw new REException(getLocalizedMessage("interval.error"),REException.REG_EBRACE,index); else return startIndex; } min = Integer.parseInt(buf.toString()); if ((unit.ch == '}') && (syntax.get(RESyntax.RE_NO_BK_BRACES) ^ unit.bk)) max = min; else if (index == input.length) if (mustMatch) throw new REException(getLocalizedMessage("interval.no.end"),REException.REG_EBRACE,index); else return startIndex; else if ((unit.ch == ',') && !unit.bk) { buf = new StringBuffer(); // Read string of digits while (((index = getCharUnit(input,index,unit)) != input.length) && Character.isDigit(unit.ch)) buf.append(unit.ch); if (!((unit.ch == '}') && (syntax.get(RESyntax.RE_NO_BK_BRACES) ^ unit.bk))) if (mustMatch) throw new REException(getLocalizedMessage("interval.error"),REException.REG_EBRACE,index); else return startIndex; // This is the case of {x,} if (buf.length() == 0) max = Integer.MAX_VALUE; else max = Integer.parseInt(buf.toString()); } else if (mustMatch) throw new REException(getLocalizedMessage("interval.error"),REException.REG_EBRACE,index); else return startIndex; // We know min and max now, and they are valid. minMax.first = min; minMax.second = max; // return the index following the '}' return index; } /** * Return a human readable form of the compiled regular expression, * useful for debugging. */ public String toString() { StringBuffer sb = new StringBuffer(); dump(sb); return sb.toString(); } void dump(StringBuffer os) { os.append('('); if (subIndex == 0) os.append("?:"); if (firstToken != null) firstToken.dumpAll(os); os.append(')'); } // Cast input appropriately or throw exception private static CharIndexed makeCharIndexed(Object input, int index) { // We could let a String fall through to final input, but since // it's the most likely input type, we check it first. if (input instanceof String) return new CharIndexedString((String) input,index); else if (input instanceof char[]) return new CharIndexedCharArray((char[]) input,index); else if (input instanceof StringBuffer) return new CharIndexedStringBuffer((StringBuffer) input,index); else if (input instanceof InputStream) return new CharIndexedInputStream((InputStream) input,index); else if (input instanceof Reader) return new CharIndexedReader((Reader) input, index); else if (input instanceof CharIndexed) return (CharIndexed) input; // do we lose index info? else return new CharIndexedString(input.toString(), index); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -