📄 regexp.java
字号:
return new RepeatElement(elem, min, max, type); } /** * Parses a regular expression character set. This method handles * the contents of the '[...]' construct in a regular expression. * * @return the element representing this character set * * @throws RegExpException if an error was encountered in the * pattern string */ private Element parseCharSet() throws RegExpException { CharacterSetElement charset; Element elem; boolean repeat = true; char start; char end; if (peekChar(0) == '^') { readChar('^'); charset = new CharacterSetElement(true); } else { charset = new CharacterSetElement(false); } while (peekChar(0) > 0 && repeat) { start = (char) peekChar(0); switch (start) { case ']': repeat = false; break; case '\\': elem = parseEscapeChar(); if (elem instanceof StringElement) { charset.addCharacters((StringElement) elem); } else { charset.addCharacterSet((CharacterSetElement) elem); } break; default: readChar(start); if (peekChar(0) == '-' && peekChar(1) > 0 && peekChar(1) != ']') { readChar('-'); end = readChar(); charset.addRange(start, end); } else { charset.addCharacter(start); } } } return charset; } /** * Parses a regular expression character. This method handles * a single normal character in a regular expression. * * @return the element representing this character * * @throws RegExpException if an error was encountered in the * pattern string */ private Element parseChar() throws RegExpException { switch (peekChar(0)) { case '\\': return parseEscapeChar(); case '^': case '$': throw new RegExpException( RegExpException.UNSUPPORTED_SPECIAL_CHARACTER, pos, pattern); default: return new StringElement(readChar()); } } /** * Parses a regular expression character escape. This method * handles a single character escape in a regular expression. * * @return the element representing this character escape * * @throws RegExpException if an error was encountered in the * pattern string */ private Element parseEscapeChar() throws RegExpException { char c; String str; readChar('\\'); c = readChar(); switch (c) { case '0': c = readChar(); if (c < '0' || c > '3') { throw new RegExpException( RegExpException.UNSUPPORTED_ESCAPE_CHARACTER, pos - 3, pattern); } str = String.valueOf(c); c = (char) peekChar(0); if ('0' <= c && c <= '7') { str += String.valueOf(readChar()); c = (char) peekChar(0); if ('0' <= c && c <= '7') { str += String.valueOf(readChar()); } } try { c = (char) Integer.parseInt(str, 8); return new StringElement(c); } catch (NumberFormatException e) { throw new RegExpException( RegExpException.UNSUPPORTED_ESCAPE_CHARACTER, pos - str.length() - 2, pattern); } case 'x': str = String.valueOf(readChar()) + String.valueOf(readChar()); try { c = (char) Integer.parseInt(str, 16); return new StringElement(c); } catch (NumberFormatException e) { throw new RegExpException( RegExpException.UNSUPPORTED_ESCAPE_CHARACTER, pos - str.length() - 2, pattern); } case 'u': str = String.valueOf(readChar()) + String.valueOf(readChar()) + String.valueOf(readChar()) + String.valueOf(readChar()); try { c = (char) Integer.parseInt(str, 16); return new StringElement(c); } catch (NumberFormatException e) { throw new RegExpException( RegExpException.UNSUPPORTED_ESCAPE_CHARACTER, pos - str.length() - 2, pattern); } case 't': return new StringElement('\t'); case 'n': return new StringElement('\n'); case 'r': return new StringElement('\r'); case 'f': return new StringElement('\f'); case 'a': return new StringElement('\u0007'); case 'e': return new StringElement('\u001B'); case 'd': return CharacterSetElement.DIGIT; case 'D': return CharacterSetElement.NON_DIGIT; case 's': return CharacterSetElement.WHITESPACE; case 'S': return CharacterSetElement.NON_WHITESPACE; case 'w': return CharacterSetElement.WORD; case 'W': return CharacterSetElement.NON_WORD; default: if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')) { throw new RegExpException( RegExpException.UNSUPPORTED_ESCAPE_CHARACTER, pos - 2, pattern); } return new StringElement(c); } } /** * Reads a number from the pattern. If the next character isn't a * numeric character, an exception is thrown. This method reads * several consecutive numeric characters. * * @return the numeric value read * * @throws RegExpException if an error was encountered in the * pattern string */ private int readNumber() throws RegExpException { StringBuffer buf = new StringBuffer(); int c; c = peekChar(0); while ('0' <= c && c <= '9') { buf.append(readChar()); c = peekChar(0); } if (buf.length() <= 0) { throw new RegExpException( RegExpException.UNEXPECTED_CHARACTER, pos, pattern); } return Integer.parseInt(buf.toString()); } /** * Reads the next character in the pattern. If no next character * exists, an exception is thrown. * * @return the character read * * @throws RegExpException if no next character was available in * the pattern string */ private char readChar() throws RegExpException { int c = peekChar(0); if (c < 0) { throw new RegExpException( RegExpException.UNTERMINATED_PATTERN, pos, pattern); } else { pos++; return (char) c; } } /** * Reads the next character in the pattern. If the character * wasn't the specified one, an exception is thrown. * * @param c the character to read * * @return the character read * * @throws RegExpException if the character read didn't match the * specified one, or if no next character was * available in the pattern string */ private char readChar(char c) throws RegExpException { if (c != readChar()) { throw new RegExpException( RegExpException.UNEXPECTED_CHARACTER, pos - 1, pattern); } return c; } /** * Returns a character that has not yet been read from the * pattern. If the requested position is beyond the end of the * pattern string, -1 is returned. * * @param count the preview position, from zero (0) * * @return the character found, or * -1 if beyond the end of the pattern string */ private int peekChar(int count) { if (pos + count < pattern.length()) { return pattern.charAt(pos + count); } else { return -1; } } /** * Combines a list of elements. This method takes care to always * concatenate adjacent string elements into a single string * element. * * @param list the list with elements * * @return the combined element */ private Element combineElements(ArrayList list) { Element prev; Element elem; String str; int i; // Concatenate string elements prev = (Element) list.get(0); for (i = 1; i < list.size(); i++) { elem = (Element) list.get(i); if (prev instanceof StringElement && elem instanceof StringElement) { str = ((StringElement) prev).getString() + ((StringElement) elem).getString(); elem = new StringElement(str); list.remove(i); list.set(i - 1, elem); i--; } prev = elem; } // Combine all remaining elements elem = (Element) list.get(list.size() - 1); for (i = list.size() - 2; i >= 0; i--) { prev = (Element) list.get(i); elem = new CombineElement(prev, elem); } return elem; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -