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

📄 regexobject.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Copyright (c) Corporation for National Research Initiativespackage org.python.modules;import org.python.core.*;import org.apache.oro.text.regex.*;public class RegexObject extends PyObject{    private static Perl5Compiler compiler = new Perl5Compiler();    private static synchronized Pattern compile(String pattern, int flags) {        try {            return compiler.compile(pattern, flags);        }        catch (MalformedPatternException e) {            throw re.ReError(e.getMessage());        }    }    private static synchronized Perl5Matcher getMatcher() {        Perl5Matcher matcher = new Perl5Matcher();        //matcher.setMultiline(false);        return matcher;    }    public String pattern;    public int flags;    public PyDictionary groupindex;    private Pattern code;    public RegexObject(String pattern, int flags) {        this.pattern = pattern;        this.flags = flags;        groupindex = new PyDictionary();        code = compile(fixPattern(pattern), flags);    }    public MatchObject match(String string) {        MatchResult result = doMatch(string);        if (result == null)            return null;        return new MatchObject(this, string, 0, string.length(), result);    }    public MatchObject match(String s, int pos) {        return match(s, pos, s.length());    }    public MatchObject match(String string, int pos, int endpos) {        if (endpos > string.length())            endpos = string.length();        if (endpos < pos)            endpos = pos;        MatchResult result =            doMatch(new PatternMatcherInput(string, pos, endpos-pos));        if (result == null)            return null;        return new MatchObject(this, string, pos, endpos, result);    }    private MatchResult doMatch(Object input) {        Perl5Matcher matcher = getMatcher();        if (input instanceof String) {            if (!matcher.matchesPrefix((String)input, code))                return null;        }        else {            if (!matcher.matchesPrefix((PatternMatcherInput)input, code))                return null;        }        return matcher.getMatch();    }    public MatchObject search(String string) {        MatchResult result = doSearch(string);        if (result == null)            return null;        return new MatchObject(this, string, 0, string.length(), result);    }    public MatchObject search(String s, int pos) {        return search(s, pos, s.length());    }    public MatchObject search(String string, int pos, int endpos) {        if (endpos > string.length())            endpos = string.length();        if (endpos < pos)            endpos = pos;        MatchResult result =            doSearch(new PatternMatcherInput(string, pos, endpos-pos));        if (result == null)            return null;        return new MatchObject(this, string, pos, endpos, result);    }    private MatchResult doSearch(Object input) {        Perl5Matcher matcher = getMatcher();        if (input instanceof String) {            if (!matcher.contains((String)input, code))                return null;        }        else {            if (!matcher.contains((PatternMatcherInput)input, code))                return null;        }        return matcher.getMatch();    }    public PyString sub(PyObject repl, String string) {        return sub(repl, string, 0);    }    public PyString sub(PyObject repl, String string, int count) {        return (PyString)subn(repl, string, count).__getitem__(0);    }    public PyTuple subn(PyObject repl, String string) {        return subn(repl, string, 0);    }    public PyTuple subn(PyObject repl, String string, int count) {        // Real work is done here        String srepl = null;        boolean expand = false;        if (repl instanceof PyString) {            srepl = repl.toString();            expand = (srepl.indexOf('\\') != -1);        }        if (count < 0) {            throw re.ReError("negative substitution count");        }        if (count == 0) {            count = Integer.MAX_VALUE;        }        // How to handle repl as String vs. callable?        int n=0;        StringBuffer buf = new StringBuffer();        Perl5Matcher matcher = getMatcher();        PatternMatcherInput match = new PatternMatcherInput(string);        int lastmatch = 0;        while (n < count && !match.endOfInput()) {            if (!matcher.contains(match, code))                break;            n++;            int offset = match.getMatchBeginOffset();            //System.err.println("off: "+offset+", "+lastmatch);            if (offset > lastmatch) {                buf.append(match.substring(lastmatch, offset));            }            if (srepl == null) {                MatchObject m = new MatchObject(this, string, lastmatch,                                                string.length(),                                                matcher.getMatch());                PyObject ret = repl.__call__(m);                buf.append(ret.toString());            }            else {                if (expand)                    buf.append(expandMatch(matcher.getMatch(), srepl));                else                    buf.append(srepl);            }            lastmatch = match.getMatchEndOffset();        }        if (lastmatch < match.getEndOffset()) {            buf.append(match.substring(lastmatch, match.getEndOffset()));        }        return new PyTuple(            new PyObject[] {                new PyString(buf.toString()),                new PyInteger(n)            });    }    public PyList split(String string) {        return split(string, 0);    }    public PyList split(String string, int maxsplit) {        if (maxsplit < 0) {            throw re.ReError("maxsplit < 0");        }        if (maxsplit == 0) {            maxsplit = Integer.MAX_VALUE;        }        int n=0;        Perl5Matcher matcher = getMatcher();        PatternMatcherInput match = new PatternMatcherInput(string);        int lastmatch = 0;        PyList results = new PyList();        while (n < maxsplit && !match.endOfInput()) {            if (!matcher.contains(match, code))                break;            n++;            int begin = match.getMatchBeginOffset();            int end = match.getMatchEndOffset();            if (begin == end) {                // More needed?                continue;            }            results.append(new PyString(match.substring(lastmatch, begin)));            MatchResult m = matcher.getMatch();            int ngroups = m.groups();            if (ngroups > 1) {                for (int j=1; j<ngroups; j++) {                    String tmp = m.group(j);                    if (tmp == null) {                        results.append(Py.None);                    }                    else {                        results.append(new PyString(tmp));                    }                }            }            lastmatch = end;        }        results.append(            new PyString(match.substring(lastmatch, match.getEndOffset())));        return results;    }    private int getindex(PyString s) {        PyInteger v = (PyInteger)groupindex.__finditem__(s);        if (v == null) {            try {                v = s.__int__();            }            catch (PyException exc) {                if (!isname(s.toString()))                    throw re.ReError("illegal character in group name");                else                    throw Py.IndexError("group "+s.__repr__() +                                        " is undefined");            }        }        return v.getValue();    }    private boolean isdigit(char c) {        return '0' <= c && c <= '9';    }    private boolean isident(char c) {        return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || (c == '_');    }    private boolean isname(String name) {        int n = name.length();        if (n <= 0 || !isident(name.charAt(0)))            return false;        for (int i = 1; i < n; i++) {

⌨️ 快捷键说明

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