📄 regexobject.java
字号:
char c = name.charAt(i); if (!isident(c) && !isdigit(c)) return false; } return true; } private String fixPattern(String pattern) { char[] chars = pattern.toCharArray(); int index=0; int group=1; int lasti=0; int n = chars.length; StringBuffer buf = new StringBuffer(); while (index < n) { if (chars[index++] == '(') { // Ignore \( because these are literal parens if (index > 2 && chars[index-2] == '\\') continue; if (index < n && chars[index] == '?') { index++; if (index < n && chars[index] == 'P') { index++; if (index == n) break; char c = chars[index++]; int start = index; if (c == '<') { while (index < n && chars[index] != '>') index++; if (index == n) throw re.ReError("unmatched <"); String name = new String(chars, start, index-start); // name must be a valid Python identifier if (!isname(name)) throw re.ReError("illegal character in " + "group name"); groupindex.__setitem__(new PyString(name), new PyInteger(group)); buf.append(chars, lasti, start-3-lasti); index++; lasti = index; group++; continue; } else { if (c == '=') { while (index < n && chars[index] != ')') { c = chars[index]; if (Character.isJavaIdentifierPart(c) && c != '$') { index++; } else { throw re.ReError( "illegal character in symbol"); } } if (index == n) throw re.ReError("?P= not closed"); if (!(Character.isJavaIdentifierStart( chars[start]))) { throw re.ReError( "illegal character starting symbol"); } String name = new String(chars, start, index-start); PyString pname = new PyString(name); buf.append(chars, lasti, start-4-lasti); buf.append('\\'); buf.append(getindex(pname)); index++; lasti=index; } else { throw re.ReError("invalid ?P grouping"); } } } else { if (chars[index] == ':') continue; while (index < n && chars[index] != ')') index++; } } else { group++; } } } if (lasti > 0) { buf.append(chars, lasti, n-lasti); //System.err.println("pat: "+buf.toString()); return buf.toString(); } else { //System.err.println("untouched: "+pattern); return pattern; } } public String expandMatch(MatchResult match, String repl) { char[] chars = repl.toCharArray(); int index=0; int lasti=0; int n = chars.length; StringBuffer buf = new StringBuffer(); try { while (index<n) { //System.out.println("index: "+index+", "+n+", "+repl); if (chars[index++] == '\\') { char ch = 0; switch (chars[index++]) { case '\\': ch = '\\'; break; case 'E': case 'G': case 'L': case 'Q': case 'U': case 'l': case 'u': throw re.ReError("\\"+chars[index-1]+ " is not allowed"); case 'n': ch = '\n'; break; case 't': ch = '\t'; break; case 'r': ch = '\r'; break; case 'v': ch = '\013'; break; case 'f': ch = '\f'; break; case 'a': ch = '\007'; break; case 'b': ch = '\b'; break; case 'g': if (chars[index++] != '<') { throw re.ReError( "missing < in symbolic reference"); } int start = index; while (index < n && chars[index] != '>') index++; if (index == n) { throw re.ReError("unfinished symbolic reference"); } index++; buf.append(chars, lasti, start-3-lasti); PyString str = new PyString(new String(chars, start, index-1-start)); String tmp = match.group(getindex(str)); if (tmp == null) { throw re.ReError("group not in match: "+str); } buf.append(tmp); lasti=index; continue; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': start = index-2; int v = chars[index-1]-'0'; char ch1; if (index<n) { ch = chars[index]; if (ch >= '0' && ch <= '9') { index++; if (index < n && ch <= '7') { ch1 = chars[index]; if (ch1 >= '0' && ch1 <= '7') { v = v*64 + (ch - '0')*8 + (ch1 - '0'); buf.append(chars, lasti, index-2-lasti); buf.append((char)v); index++; lasti=index; } } v = v*10 + (ch - '0'); } } buf.append(chars, lasti, start-lasti); tmp = match.group(v); if (tmp == null) { throw re.ReError("group not in match: "+v); } buf.append(tmp); lasti=index; continue; default: continue; } buf.append(chars, lasti, index-2-lasti); buf.append(ch); lasti=index; } } } catch (ArrayIndexOutOfBoundsException exc) { throw re.ReError("invalid expression"); } if (lasti > 0) { buf.append(chars, lasti, n-lasti); return buf.toString(); } else { return repl; } } public PyList findall(String string) { Perl5Matcher matcher = getMatcher(); PatternMatcherInput match = new PatternMatcherInput(string); PyList ret = new PyList(); while (matcher.contains(match, code)) { MatchResult result = matcher.getMatch(); int groups = result.groups(); if (groups == 1) // no parenthetical subgroups ret.append(new PyString(result.group(0))); else if (groups == 2) // one parenthetical subgroup, but we only return a list of // the first matching subgroup. ret.append(new PyString(result.group(1))); else { // two or more subgroups, so ignore the whole match PyString[] submatches = new PyString[groups-1]; for (int g = 1; g < groups; g++) submatches[g-1] = new PyString(result.group(g)); PyTuple tup = new PyTuple(submatches); ret.append(tup); } } return ret; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -