|
| 1 | +import re |
| 2 | + |
| 3 | + |
| 4 | +def re_escape(fn): |
| 5 | + def arg_escaped(this, *args): |
| 6 | + t = [isinstance(a, VerEx) and a.s or re.escape(str(a)) for a in args] |
| 7 | + return fn(this, *t) |
| 8 | + return arg_escaped |
| 9 | + |
| 10 | + |
| 11 | +class VerEx(object): |
| 12 | + ''' |
| 13 | + --- VerbalExpressions class --- |
| 14 | + the following methods behave different from the original js lib! |
| 15 | +
|
| 16 | + - end_of_line |
| 17 | + - start_of_line |
| 18 | + - or |
| 19 | + when you say you want `$`, `^` and `|`, we just insert it right there. |
| 20 | + No other tricks. |
| 21 | +
|
| 22 | + And any string you inserted will be automatically grouped |
| 23 | + excepte `tab` and `add`. |
| 24 | + ''' |
| 25 | + def __init__(self): |
| 26 | + self.s = '' |
| 27 | + self.modifiers = {'I': 0, 'M': 0} |
| 28 | + |
| 29 | + def __getattr__(self, attr): |
| 30 | + ''' any other function will be sent to the regex object ''' |
| 31 | + regex = self.regex() |
| 32 | + return getattr(regex, attr) |
| 33 | + |
| 34 | + def add(self, value): |
| 35 | + self.s += value |
| 36 | + return self |
| 37 | + |
| 38 | + def regex(self): |
| 39 | + ''' get a regular expression object. ''' |
| 40 | + return re.compile(self.s, self.modifiers['I'] | self.modifiers['M']) |
| 41 | + compile = regex |
| 42 | + |
| 43 | + def source(self): |
| 44 | + ''' return the raw string''' |
| 45 | + return self.s |
| 46 | + raw = value = source |
| 47 | + |
| 48 | + # --------------------------------------------- |
| 49 | + |
| 50 | + def anything(self): |
| 51 | + return self.add('(.*)') |
| 52 | + |
| 53 | + @re_escape |
| 54 | + def anything_but(self, value): |
| 55 | + return self.add('([^' + value + ']*)') |
| 56 | + |
| 57 | + def end_of_line(self): |
| 58 | + return self.add('$') |
| 59 | + |
| 60 | + @re_escape |
| 61 | + def maybe(self, value): |
| 62 | + return self.add("(" + value + ")?") |
| 63 | + |
| 64 | + def start_of_line(self): |
| 65 | + return self.add('^') |
| 66 | + |
| 67 | + @re_escape |
| 68 | + def find(self, value): |
| 69 | + return self.add('(' + value + ')') |
| 70 | + then = find |
| 71 | + |
| 72 | + # special characters and groups |
| 73 | + |
| 74 | + @re_escape |
| 75 | + def any(self, value): |
| 76 | + return self.add("([" + value + "])") |
| 77 | + any_of = any |
| 78 | + |
| 79 | + def line_break(self): |
| 80 | + return self.add("(\\n|(\\r\\n))") |
| 81 | + br = line_break |
| 82 | + |
| 83 | + @re_escape |
| 84 | + def range(self, *args): |
| 85 | + from_tos = [args[i:i+2] for i in range(0, len(args), 2)] |
| 86 | + return self.add("([" + ''.join(['-'.join(i) for i in from_tos]) + "])") |
| 87 | + |
| 88 | + def tab(self): |
| 89 | + return self.add('\\t') |
| 90 | + |
| 91 | + def word(self): |
| 92 | + return self.add("(\\w+)") |
| 93 | + |
| 94 | + def OR(self, value=None): |
| 95 | + ''' `or` is a python keyword so we use `OR` instead. ''' |
| 96 | + self.add("|") |
| 97 | + return self.find(value) if value else self |
| 98 | + |
| 99 | + def replace(self, string, repl): |
| 100 | + return self.sub(repl, string) |
| 101 | + |
| 102 | + # --------------- modifiers ------------------------ |
| 103 | + |
| 104 | + # no global option. It depends on which method |
| 105 | + # you called on the regex object. |
| 106 | + |
| 107 | + def with_any_case(self, value=False): |
| 108 | + self.modifiers['I'] = re.I if value else 0 |
| 109 | + return self |
| 110 | + |
| 111 | + def search_one_line(self, value=False): |
| 112 | + self.modifiers['M'] = re.M if value else 0 |
| 113 | + return self |
0 commit comments