1 package org.python.modules; 3 4 import org.python.core.*; 5 import org.apache.oro.text.regex.*; 6 7 public class re implements ClassDictInit 8 { 9 public static String [] __all__ = new String [] { 10 "match","search","sub","subn","split","findall","escape","compile", 11 "I","L","M","S","X","IGNORECASE","LOCALE","MULTILINE","DOTALL", 12 "VERBOSE","error" 13 }; 14 15 public static PyObject error = new PyString("re.error"); 16 17 public static PyException ReError(String message) { 18 return new PyException(error, message); 19 } 20 21 public static void classDictInit(PyObject dict) { 22 dict.__setitem__("IGNORECASE", 23 new PyInteger(Perl5Compiler.CASE_INSENSITIVE_MASK)); 24 dict.__setitem__("I", 25 new PyInteger(Perl5Compiler.CASE_INSENSITIVE_MASK)); 26 dict.__setitem__("MULTILINE", 27 new PyInteger(Perl5Compiler.MULTILINE_MASK)); 28 dict.__setitem__("M", new PyInteger(Perl5Compiler.MULTILINE_MASK)); 29 dict.__setitem__("DOTALL", 30 new PyInteger(Perl5Compiler.SINGLELINE_MASK)); 31 dict.__setitem__("S", new PyInteger(Perl5Compiler.SINGLELINE_MASK)); 32 dict.__setitem__("VERBOSE", 33 new PyInteger(Perl5Compiler.EXTENDED_MASK)); 34 dict.__setitem__("X", new PyInteger(Perl5Compiler.EXTENDED_MASK)); 35 dict.__setitem__("LOCALE", new PyInteger(0)); 37 dict.__setitem__("L", new PyInteger(0)); 38 dict.__setitem__("UNICODE", new PyInteger(0)); 39 dict.__setitem__("U", new PyInteger(0)); 40 } 41 42 private static RegexObject cachecompile(String pattern, int flags) { 44 return compile(pattern, flags); 45 } 46 47 private static RegexObject cachecompile(String pattern) { 48 return cachecompile(pattern, 0); 49 } 50 51 public static MatchObject match(String pattern, String string) { 52 return match(pattern, string, 0); 53 } 54 55 public static MatchObject match(String pattern, String string, 56 int flags) 57 { 58 return cachecompile(pattern, flags).match(string); 59 } 60 61 public static MatchObject search(String pattern, String string) { 62 return search(pattern, string, 0); 63 } 64 65 public static MatchObject search(String pattern, String string, int flags) 66 { 67 return cachecompile(pattern, flags).search(string); 68 } 69 70 private static RegexObject getPattern(PyObject pattern) { 71 if (pattern instanceof PyString) { 72 return cachecompile(pattern.toString()); 73 } else if (pattern instanceof RegexObject) { 74 return (RegexObject)pattern; 75 } 76 throw Py.TypeError("pattern must be string or RegexObject"); 77 } 78 79 public static PyString sub(PyObject pattern, PyObject repl, String string) 80 { 81 return sub(pattern, repl, string, 0); 82 } 83 84 public static PyString sub(PyObject pattern, PyObject repl, 85 String string, int count) 86 { 87 return getPattern(pattern).sub(repl, string, count); 88 } 89 90 public static PyTuple subn(PyObject pattern, PyObject repl, String string) 91 { 92 return subn(pattern, repl, string, 0); 93 } 94 95 public static PyTuple subn(PyObject pattern, PyObject repl, 96 String string, int count) 97 { 98 return getPattern(pattern).subn(repl, string, count); 99 } 100 101 public static PyList split(PyObject pattern, String string) { 102 return split(pattern, string, 0); 103 } 104 105 public static PyList split(PyObject pattern, String string, 106 int maxsplit) 107 { 108 return getPattern(pattern).split(string, maxsplit); 109 } 110 111 public static PyList findall(PyObject pattern, String string) { 112 return getPattern(pattern).findall(string); 113 } 114 115 public static String escape(String s) { 116 return Perl5Compiler.quotemeta(s); 117 } 118 119 public static RegexObject compile(String pattern) { 120 return new RegexObject(pattern, 0); 121 } 122 123 public static RegexObject compile(String pattern, int flags) { 124 return new RegexObject(pattern, flags); 125 } 126 } 127 | Popular Tags |