📄 perl5util.java
字号:
* <p> * Of special note is that this split method performs EXACTLY the same * as the Perl split() function. In other words, if the split pattern * contains parentheses, additional Vector elements are created from * each of the matching subgroups in the pattern. Using an example * similar to the one from the Camel book: * <blockquote><pre> * split("/([,-])/", "8-12,15,18") * </pre></blockquote> * produces the Vector containing: * <blockquote><pre> * { "8", "-", "12", ",", "15", ",", "18" } * </pre></blockquote> * The Util.split() method in the * OROMatcher<font size="-2"><sup>TM</sup></font> package does NOT * implement this particular behavior because it is intended to * be usable with Pattern instances other than Perl5Pattern. * <p> * @deprecated Use * {@link split(List results, String pattern, String input, int limit)} * instead. * @param pattern The regular expression to use as a split delimiter. * @param input The String to split. * @param limit The limit on the size of the returned <code>Vector</code>. * Values <= 0 produce the same behavior as the SPLIT_ALL constant which * causes the limit to be ignored and splits to be performed on all * occurrences of the pattern. You should use the SPLIT_ALL constant * to achieve this behavior instead of relying on the default behavior * associated with non-positive limit values. * @return A <code> Vector </code> containing the substrings of the input * that occur between the regular expression delimiter occurences. The * input will not be split into any more substrings than the specified * limit. A way of thinking of this is that only the first * <b>limit - 1</b> * matches of the delimiting regular expression will be used to split the * input. * @exception MalformedPerl5PatternException If there is an error in * the expression. You are not forced to catch this exception * because it is derived from RuntimeException. */ public synchronized Vector split(String pattern, String input, int limit) throws MalformedPerl5PatternException { int beginOffset, groups, index; String group; Vector results = new Vector(20); MatchResult currentResult = null; PatternMatcherInput pinput; Pattern compiledPattern; compiledPattern = __parseMatchExpression(pattern); pinput = new PatternMatcherInput(input); beginOffset = 0; while(--limit != 0 && __matcher.contains(pinput, compiledPattern)) { currentResult = __matcher.getMatch(); results.addElement(input.substring(beginOffset, currentResult.beginOffset(0))); if((groups = currentResult.groups()) > 1) { for(index = 1; index < groups; ++index) { group = currentResult.group(index); if(group != null && group.length() > 0) results.addElement(group); } } beginOffset = currentResult.endOffset(0); } results.addElement(input.substring(beginOffset, input.length())); // Just for the sake of completeness __lastMatch = currentResult; return results; } /** * This method is identical to calling: * <blockquote><pre> * split(pattern, input, SPLIT_ALL); * </pre></blockquote> * @deprecated Use {@link split(List results, String pattern, String input)} * instead. */ public synchronized Vector split(String pattern, String input) throws MalformedPerl5PatternException { return split(pattern, input, SPLIT_ALL); } /** * Splits input in the default Perl manner, splitting on all whitespace. * This method is identical to calling: * <blockquote><pre> * split("/\\s+/", input); * </pre></blockquote> */ public synchronized Vector split(String input) throws MalformedPerl5PatternException { return split("/\\s+/", input); } // // MatchResult interface methods. // /** * Returns the length of the last match found. * <p> * @return The length of the last match found. */ public synchronized int length() { return __lastMatch.length(); } /** * @return The number of groups contained in the last match found. * This number includes the 0th group. In other words, the * result refers to the number of parenthesized subgroups plus * the entire match itself. */ public synchronized int groups() { return __lastMatch.groups(); } /** * Returns the contents of the parenthesized subgroups of the last match * found according to the behavior dictated by the MatchResult interface. * <p> * @param group The pattern subgroup to return. * @return A string containing the indicated pattern subgroup. Group * 0 always refers to the entire match. If a group was never * matched, it returns null. This is not to be confused with * a group matching the null string, which will return a String * of length 0. */ public synchronized String group(int group) { return __lastMatch.group(group); } /** * Returns the begin offset of the subgroup of the last match found * relative the beginning of the match. * <p> * @param group The pattern subgroup. * @return The offset into group 0 of the first token in the indicated * pattern subgroup. If a group was never matched or does * not exist, returns -1. Be aware that a group that matches * the null string at the end of a match will have an offset * equal to the length of the string, so you shouldn't blindly * use the offset to index an array or String. */ public synchronized int begin(int group) { return __lastMatch.begin(group); } /** * Returns the end offset of the subgroup of the last match found * relative the beginning of the match. * <p> * @param group The pattern subgroup. * @return Returns one plus the offset into group 0 of the last token in * the indicated pattern subgroup. If a group was never matched * or does not exist, returns -1. A group matching the null * string will return its start offset. */ public synchronized int end(int group) { return __lastMatch.end(group); } /** * Returns an offset marking the beginning of the last pattern match * found relative to the beginning of the input from which the match * was extracted. * <p> * @param group The pattern subgroup. * @return The offset of the first token in the indicated * pattern subgroup. If a group was never matched or does * not exist, returns -1. */ public synchronized int beginOffset(int group) { return __lastMatch.beginOffset(group); } /** * Returns an offset marking the end of the last pattern match found * relative to the beginning of the input from which the match was * extracted. * <p> * @param group The pattern subgroup. * @return Returns one plus the offset of the last token in * the indicated pattern subgroup. If a group was never matched * or does not exist, returns -1. A group matching the null * string will return its start offset. */ public synchronized int endOffset(int group) { return __lastMatch.endOffset(group); } /** * Returns the same as group(0). * <p> * @return A string containing the entire match. */ public synchronized String toString() { return __lastMatch.toString(); } /** * Returns the part of the input preceding that last match found. * <p> * @return The part of the input following the last match found. */ public synchronized String preMatch() { int begin; if(__originalInput == null) return __nullString; begin = __lastMatch.beginOffset(0); if(begin <= 0) return __nullString; if(__originalInput instanceof char[]) { char[] input; input = (char[])__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(begin > input.length) begin = input.length; return new String(input, __inputBeginOffset, begin); } else if(__originalInput instanceof String) { String input; input = (String)__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(begin > input.length()) begin = input.length(); return input.substring(__inputBeginOffset, begin); } return __nullString; } /** * Returns the part of the input following that last match found. * <p> * @return The part of the input following the last match found. */ public synchronized String postMatch() { int end; if(__originalInput == null) return __nullString; end = __lastMatch.endOffset(0); if(end < 0) return __nullString; if(__originalInput instanceof char[]) { char[] input; input = (char[])__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(end >= input.length) return __nullString; return new String(input, end, __inputEndOffset - end); } else if(__originalInput instanceof String) { String input; input = (String)__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(end >= input.length()) return __nullString; return input.substring(end, __inputEndOffset); } return __nullString; } /** * Returns the part of the input preceding that last match found as a * char array. This method eliminates the extra * buffer copying caused by preMatch().toCharArray(). * <p> * @return The part of the input following the last match found as a char[]. * If the result is of zero length, returns null instead of a zero * length array. */ public synchronized char[] preMatchCharArray() { int begin; char[] result = null; if(__originalInput == null) return null; begin = __lastMatch.beginOffset(0); if(begin <= 0) return null; if(__originalInput instanceof char[]) { char[] input; input = (char[])__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(begin >= input.length) begin = input.length; result = new char[begin - __inputBeginOffset]; System.arraycopy(input, __inputBeginOffset, result, 0, result.length); } else if(__originalInput instanceof String) { String input; input = (String)__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(begin >= input.length()) begin = input.length(); result = new char[begin - __inputBeginOffset]; input.getChars(__inputBeginOffset, begin, result, 0); } return result; } /** * Returns the part of the input following that last match found as a char * array. This method eliminates the extra buffer copying caused by * preMatch().toCharArray(). * <p> * @return The part of the input following the last match found as a char[]. * If the result is of zero length, returns null instead of a zero * length array. */ public synchronized char[] postMatchCharArray() { int end; char[] result = null; if(__originalInput == null) return null; end = __lastMatch.endOffset(0); if(end < 0) return null; if(__originalInput instanceof char[]) { int length; char[] input; input = (char[])__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(end >= input.length) return null; length = __inputEndOffset - end; result = new char[length]; System.arraycopy(input, end, result, 0, length); } else if(__originalInput instanceof String) { String input; input = (String)__originalInput; // Just in case we make sure begin offset is in bounds. It should // be but we're paranoid. if(end >= __inputEndOffset) return null; result = new char[__inputEndOffset - end]; input.getChars(end, __inputEndOffset, result, 0); } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -