• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Secret Labs' Regular Expression Engine
3#
4# re-compatible interface for the sre matching engine
5#
6# Copyright (c) 1998-2001 by Secret Labs AB.  All rights reserved.
7#
8# This version of the SRE library can be redistributed under CNRI's
9# Python 1.6 license.  For any other use, please contact Secret Labs
10# AB (info@pythonware.com).
11#
12# Portions of this engine have been developed in cooperation with
13# CNRI.  Hewlett-Packard provided funding for 1.6 integration and
14# other compatibility work.
15#
16
17r"""Support for regular expressions (RE).
18
19This module provides regular expression matching operations similar to
20those found in Perl.  It supports both 8-bit and Unicode strings; both
21the pattern and the strings being processed can contain null bytes and
22characters outside the US ASCII range.
23
24Regular expressions can contain both special and ordinary characters.
25Most ordinary characters, like "A", "a", or "0", are the simplest
26regular expressions; they simply match themselves.  You can
27concatenate ordinary characters, so last matches the string 'last'.
28
29The special characters are:
30    "."      Matches any character except a newline.
31    "^"      Matches the start of the string.
32    "$"      Matches the end of the string or just before the newline at
33             the end of the string.
34    "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
35             Greedy means that it will match as many repetitions as possible.
36    "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
37    "?"      Matches 0 or 1 (greedy) of the preceding RE.
38    *?,+?,?? Non-greedy versions of the previous three special characters.
39    {m,n}    Matches from m to n repetitions of the preceding RE.
40    {m,n}?   Non-greedy version of the above.
41    "\\"     Either escapes special characters or signals a special sequence.
42    []       Indicates a set of characters.
43             A "^" as the first character indicates a complementing set.
44    "|"      A|B, creates an RE that will match either A or B.
45    (...)    Matches the RE inside the parentheses.
46             The contents can be retrieved or matched later in the string.
47    (?aiLmsux) The letters set the corresponding flags defined below.
48    (?:...)  Non-grouping version of regular parentheses.
49    (?P<name>...) The substring matched by the group is accessible by name.
50    (?P=name)     Matches the text matched earlier by the group named name.
51    (?#...)  A comment; ignored.
52    (?=...)  Matches if ... matches next, but doesn't consume the string.
53    (?!...)  Matches if ... doesn't match next.
54    (?<=...) Matches if preceded by ... (must be fixed length).
55    (?<!...) Matches if not preceded by ... (must be fixed length).
56    (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
57                       the (optional) no pattern otherwise.
58
59The special sequences consist of "\\" and a character from the list
60below.  If the ordinary character is not on the list, then the
61resulting RE will match the second character.
62    \number  Matches the contents of the group of the same number.
63    \A       Matches only at the start of the string.
64    \Z       Matches only at the end of the string.
65    \b       Matches the empty string, but only at the start or end of a word.
66    \B       Matches the empty string, but not at the start or end of a word.
67    \d       Matches any decimal digit; equivalent to the set [0-9] in
68             bytes patterns or string patterns with the ASCII flag.
69             In string patterns without the ASCII flag, it will match the whole
70             range of Unicode digits.
71    \D       Matches any non-digit character; equivalent to [^\d].
72    \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
73             bytes patterns or string patterns with the ASCII flag.
74             In string patterns without the ASCII flag, it will match the whole
75             range of Unicode whitespace characters.
76    \S       Matches any non-whitespace character; equivalent to [^\s].
77    \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
78             in bytes patterns or string patterns with the ASCII flag.
79             In string patterns without the ASCII flag, it will match the
80             range of Unicode alphanumeric characters (letters plus digits
81             plus underscore).
82             With LOCALE, it will match the set [0-9_] plus characters defined
83             as letters for the current locale.
84    \W       Matches the complement of \w.
85    \\       Matches a literal backslash.
86
87This module exports the following functions:
88    match     Match a regular expression pattern to the beginning of a string.
89    fullmatch Match a regular expression pattern to all of a string.
90    search    Search a string for the presence of a pattern.
91    sub       Substitute occurrences of a pattern found in a string.
92    subn      Same as sub, but also return the number of substitutions made.
93    split     Split a string by the occurrences of a pattern.
94    findall   Find all occurrences of a pattern in a string.
95    finditer  Return an iterator yielding a Match object for each match.
96    compile   Compile a pattern into a Pattern object.
97    purge     Clear the regular expression cache.
98    escape    Backslash all non-alphanumerics in a string.
99
100Each function other than purge and escape can take an optional 'flags' argument
101consisting of one or more of the following module constants, joined by "|".
102A, L, and U are mutually exclusive.
103    A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \D
104                   match the corresponding ASCII character categories
105                   (rather than the whole Unicode categories, which is the
106                   default).
107                   For bytes patterns, this flag is the only available
108                   behaviour and needn't be specified.
109    I  IGNORECASE  Perform case-insensitive matching.
110    L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.
111    M  MULTILINE   "^" matches the beginning of lines (after a newline)
112                   as well as the string.
113                   "$" matches the end of lines (before a newline) as well
114                   as the end of the string.
115    S  DOTALL      "." matches any character at all, including the newline.
116    X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.
117    U  UNICODE     For compatibility only. Ignored for string patterns (it
118                   is the default), and forbidden for bytes patterns.
119
120This module also defines an exception 'error'.
121
122"""
123
124import enum
125import sre_compile
126import sre_parse
127import functools
128try:
129    import _locale
130except ImportError:
131    _locale = None
132
133
134# public symbols
135__all__ = [
136    "match", "fullmatch", "search", "sub", "subn", "split",
137    "findall", "finditer", "compile", "purge", "template", "escape",
138    "error", "Pattern", "Match", "A", "I", "L", "M", "S", "X", "U",
139    "ASCII", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
140    "UNICODE",
141]
142
143__version__ = "2.2.1"
144
145class RegexFlag(enum.IntFlag):
146    ASCII = A = sre_compile.SRE_FLAG_ASCII # assume ascii "locale"
147    IGNORECASE = I = sre_compile.SRE_FLAG_IGNORECASE # ignore case
148    LOCALE = L = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale
149    UNICODE = U = sre_compile.SRE_FLAG_UNICODE # assume unicode "locale"
150    MULTILINE = M = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline
151    DOTALL = S = sre_compile.SRE_FLAG_DOTALL # make dot match newline
152    VERBOSE = X = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments
153    # sre extensions (experimental, don't rely on these)
154    TEMPLATE = T = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking
155    DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation
156
157    def __repr__(self):
158        if self._name_ is not None:
159            return f're.{self._name_}'
160        value = self._value_
161        members = []
162        negative = value < 0
163        if negative:
164            value = ~value
165        for m in self.__class__:
166            if value & m._value_:
167                value &= ~m._value_
168                members.append(f're.{m._name_}')
169        if value:
170            members.append(hex(value))
171        res = '|'.join(members)
172        if negative:
173            if len(members) > 1:
174                res = f'~({res})'
175            else:
176                res = f'~{res}'
177        return res
178    __str__ = object.__str__
179globals().update(RegexFlag.__members__)
180
181# sre exception
182error = sre_compile.error
183
184# --------------------------------------------------------------------
185# public interface
186
187def match(pattern, string, flags=0):
188    """Try to apply the pattern at the start of the string, returning
189    a Match object, or None if no match was found."""
190    return _compile(pattern, flags).match(string)
191
192def fullmatch(pattern, string, flags=0):
193    """Try to apply the pattern to all of the string, returning
194    a Match object, or None if no match was found."""
195    return _compile(pattern, flags).fullmatch(string)
196
197def search(pattern, string, flags=0):
198    """Scan through string looking for a match to the pattern, returning
199    a Match object, or None if no match was found."""
200    return _compile(pattern, flags).search(string)
201
202def sub(pattern, repl, string, count=0, flags=0):
203    """Return the string obtained by replacing the leftmost
204    non-overlapping occurrences of the pattern in string by the
205    replacement repl.  repl can be either a string or a callable;
206    if a string, backslash escapes in it are processed.  If it is
207    a callable, it's passed the Match object and must return
208    a replacement string to be used."""
209    return _compile(pattern, flags).sub(repl, string, count)
210
211def subn(pattern, repl, string, count=0, flags=0):
212    """Return a 2-tuple containing (new_string, number).
213    new_string is the string obtained by replacing the leftmost
214    non-overlapping occurrences of the pattern in the source
215    string by the replacement repl.  number is the number of
216    substitutions that were made. repl can be either a string or a
217    callable; if a string, backslash escapes in it are processed.
218    If it is a callable, it's passed the Match object and must
219    return a replacement string to be used."""
220    return _compile(pattern, flags).subn(repl, string, count)
221
222def split(pattern, string, maxsplit=0, flags=0):
223    """Split the source string by the occurrences of the pattern,
224    returning a list containing the resulting substrings.  If
225    capturing parentheses are used in pattern, then the text of all
226    groups in the pattern are also returned as part of the resulting
227    list.  If maxsplit is nonzero, at most maxsplit splits occur,
228    and the remainder of the string is returned as the final element
229    of the list."""
230    return _compile(pattern, flags).split(string, maxsplit)
231
232def findall(pattern, string, flags=0):
233    """Return a list of all non-overlapping matches in the string.
234
235    If one or more capturing groups are present in the pattern, return
236    a list of groups; this will be a list of tuples if the pattern
237    has more than one group.
238
239    Empty matches are included in the result."""
240    return _compile(pattern, flags).findall(string)
241
242def finditer(pattern, string, flags=0):
243    """Return an iterator over all non-overlapping matches in the
244    string.  For each match, the iterator returns a Match object.
245
246    Empty matches are included in the result."""
247    return _compile(pattern, flags).finditer(string)
248
249def compile(pattern, flags=0):
250    "Compile a regular expression pattern, returning a Pattern object."
251    return _compile(pattern, flags)
252
253def purge():
254    "Clear the regular expression caches"
255    _cache.clear()
256    _compile_repl.cache_clear()
257
258def template(pattern, flags=0):
259    "Compile a template pattern, returning a Pattern object"
260    return _compile(pattern, flags|T)
261
262# SPECIAL_CHARS
263# closing ')', '}' and ']'
264# '-' (a range in character set)
265# '&', '~', (extended character set operations)
266# '#' (comment) and WHITESPACE (ignored) in verbose mode
267_special_chars_map = {i: '\\' + chr(i) for i in b'()[]{}?*+-|^$\\.&~# \t\n\r\v\f'}
268
269def escape(pattern):
270    """
271    Escape special characters in a string.
272    """
273    if isinstance(pattern, str):
274        return pattern.translate(_special_chars_map)
275    else:
276        pattern = str(pattern, 'latin1')
277        return pattern.translate(_special_chars_map).encode('latin1')
278
279Pattern = type(sre_compile.compile('', 0))
280Match = type(sre_compile.compile('', 0).match(''))
281
282# --------------------------------------------------------------------
283# internals
284
285_cache = {}  # ordered!
286
287_MAXCACHE = 512
288def _compile(pattern, flags):
289    # internal: compile pattern
290    if isinstance(flags, RegexFlag):
291        flags = flags.value
292    try:
293        return _cache[type(pattern), pattern, flags]
294    except KeyError:
295        pass
296    if isinstance(pattern, Pattern):
297        if flags:
298            raise ValueError(
299                "cannot process flags argument with a compiled pattern")
300        return pattern
301    if not sre_compile.isstring(pattern):
302        raise TypeError("first argument must be string or compiled pattern")
303    p = sre_compile.compile(pattern, flags)
304    if not (flags & DEBUG):
305        if len(_cache) >= _MAXCACHE:
306            # Drop the oldest item
307            try:
308                del _cache[next(iter(_cache))]
309            except (StopIteration, RuntimeError, KeyError):
310                pass
311        _cache[type(pattern), pattern, flags] = p
312    return p
313
314@functools.lru_cache(_MAXCACHE)
315def _compile_repl(repl, pattern):
316    # internal: compile replacement pattern
317    return sre_parse.parse_template(repl, pattern)
318
319def _expand(pattern, match, template):
320    # internal: Match.expand implementation hook
321    template = sre_parse.parse_template(template, pattern)
322    return sre_parse.expand_template(template, match)
323
324def _subx(pattern, template):
325    # internal: Pattern.sub/subn implementation helper
326    template = _compile_repl(template, pattern)
327    if not template[0] and len(template[1]) == 1:
328        # literal replacement
329        return template[1][0]
330    def filter(match, template=template):
331        return sre_parse.expand_template(template, match)
332    return filter
333
334# register myself for pickling
335
336import copyreg
337
338def _pickle(p):
339    return _compile, (p.pattern, p.flags)
340
341copyreg.pickle(Pattern, _pickle, _compile)
342
343# --------------------------------------------------------------------
344# experimental stuff (see python-dev discussions for details)
345
346class Scanner:
347    def __init__(self, lexicon, flags=0):
348        from sre_constants import BRANCH, SUBPATTERN
349        if isinstance(flags, RegexFlag):
350            flags = flags.value
351        self.lexicon = lexicon
352        # combine phrases into a compound pattern
353        p = []
354        s = sre_parse.State()
355        s.flags = flags
356        for phrase, action in lexicon:
357            gid = s.opengroup()
358            p.append(sre_parse.SubPattern(s, [
359                (SUBPATTERN, (gid, 0, 0, sre_parse.parse(phrase, flags))),
360                ]))
361            s.closegroup(gid, p[-1])
362        p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
363        self.scanner = sre_compile.compile(p)
364    def scan(self, string):
365        result = []
366        append = result.append
367        match = self.scanner.scanner(string).match
368        i = 0
369        while True:
370            m = match()
371            if not m:
372                break
373            j = m.end()
374            if i == j:
375                break
376            action = self.lexicon[m.lastindex-1][1]
377            if callable(action):
378                self.match = m
379                action = action(self, m.group())
380            if action is not None:
381                append(action)
382            i = j
383        return result, string[i:]
384