A compiled regular expression for matching Unicode strings. It is represented as either a sequence of bytecode instructions (dynamic) or as a specialized Rust function (native). It can be used to search, split or replace text. All searching is done with an implicit .*? at the begin
| 35 | |
| 36 | |
| 37 | class Rure(object): |
| 38 | """ A compiled regular expression for matching Unicode strings. |
| 39 | |
| 40 | It is represented as either a sequence of bytecode instructions (dynamic) |
| 41 | or as a specialized Rust function (native). It can be used to search, |
| 42 | split or replace text. All searching is done with an implicit .*? |
| 43 | at the beginning and end of an expression. To force an expression to match |
| 44 | the whole string (or a prefix or a suffix), you must use an anchor |
| 45 | like ^ or $ (or \A and \z). |
| 46 | |
| 47 | While this crate will handle Unicode strings (whether in the regular |
| 48 | expression or in the search text), all positions returned are byte indices. |
| 49 | Every byte index is guaranteed to be at a Unicode code point boundary. |
| 50 | """ |
| 51 | |
| 52 | def __init__(self, re, _pointer=None, |
| 53 | flags=DEFAULT_FLAGS, **options): |
| 54 | """ Compiles a regular expression. Once compiled, it can be used |
| 55 | repeatedly to search, split or replace text in a string. |
| 56 | |
| 57 | :param re: Bytestring expression to compile |
| 58 | :param flags: Bitmask of flags |
| 59 | :param kwargs: Config options to pass (size_limit, dfa_size_limit) |
| 60 | """ |
| 61 | if not isinstance(re, bytes): |
| 62 | raise TypeError("'rure.lib.Rure' must be instantiated with a " |
| 63 | "bytestring as first argument.") |
| 64 | |
| 65 | self._err = _native.ffi.gc(_native.lib.rure_error_new(), _native.lib.rure_error_free) |
| 66 | self._opts = _native.ffi.gc(_native.lib.rure_options_new(), _native.lib.rure_options_free) |
| 67 | |
| 68 | self.options = options |
| 69 | if 'size_limit' in options: |
| 70 | _native.lib.rure_options_size_limit(self._opts, options['size_limit']) |
| 71 | if 'dfa_size_limit' in options: |
| 72 | _native.lib.rure_options_dfa_size_limit(self._opts, |
| 73 | options['dfa_size_limit']) |
| 74 | |
| 75 | if _pointer is None: |
| 76 | s = checked_call( |
| 77 | _native.lib.rure_compile, |
| 78 | self._err, |
| 79 | re, |
| 80 | len(re), |
| 81 | flags, |
| 82 | self._opts |
| 83 | ) |
| 84 | else: |
| 85 | s = _pointer |
| 86 | self._ptr = _native.ffi.gc(s, _native.lib.rure_free) |
| 87 | self.capture_cls = namedtuple( |
| 88 | 'Captures', |
| 89 | [i.decode('utf8') if i else u'' for i in self.capture_names()], |
| 90 | rename=True |
| 91 | ) |
| 92 | |
| 93 | @accepts_bytes |
| 94 | def capture_name_index(self, name): |
no outgoing calls