def convert(s, syntax=None):
    """Convert a regex regular expression to re syntax.

    The first argument is the regular expression, as a string object,
    just like it would be passed to regex.compile().  (I.e., pass the
    actual string object -- string quotes must already have been
    removed and the standard escape processing has already been done,
    e.g. by eval().)

    The optional second argument is the regex syntax variant to be
    used.  This is an integer mask as passed to regex.set_syntax();
    the flag bits are defined in regex_syntax.  When not specified, or
    when None is given, the current regex syntax mask (as retrieved by
    regex.get_syntax()) is used -- which is 0 by default.

    The return value is a regular expression, as a string object that
    could be passed to re.compile().  (I.e., no string quotes have
    been added -- use quote() below, or repr().)

    The conversion is not always guaranteed to be correct.  More
    syntactical analysis should be performed to detect borderline
    cases and decide what to do with them.  For example, 'x*?' is not
    translated correctly.

    """
    table = mastertable.copy()
    if syntax is None:
        syntax = regex.get_syntax()
    if syntax & RE_NO_BK_PARENS:
        del table[r'\('], table[r'\)']
        del table['('], table[')']
    if syntax & RE_NO_BK_VBAR:
        del table[r'\|']
        del table['|']
    if syntax & RE_BK_PLUS_QM:
        table['+'] = r'\+'
        table['?'] = r'\?'
        table[r'\+'] = '+'
        table[r'\?'] = '?'
    if syntax & RE_NEWLINE_OR:
        table['\n'] = '|'
    res = ""

    i = 0
    end = len(s)
    while i < end:
        c = s[i]
        i = i+1
        if c == '\\':
            c = s[i]
            i = i+1
            key = '\\' + c
            key = table.get(key, key)
            res = res + key
        else:
            c = table.get(c, c)
            res = res + c
    return res
Exemple #2
0
def compile(pat):
    if type(pat) != type(''):
        return pat              # Assume it is a compiled regex
    key = (pat, regex.get_syntax())
    if key in cache:
        prog = cache[key]       # Get it from the cache
    else:
        prog = cache[key] = regex.compile(pat)
    return prog
def compile(pat):
    if type(pat) != type(''):
        return pat  # Assume it is a compiled regex
    key = (pat, regex.get_syntax())
    if key in cache:
        prog = cache[key]  # Get it from the cache
    else:
        prog = cache[key] = regex.compile(pat)
    return prog
Exemple #4
0
def compile(pat):
    if type(pat) <> type(""):
        return pat  # Assume it is a compiled regex
    key = (pat, regex.get_syntax())
    if cache.has_key(key):
        prog = cache[key]  # Get it from the cache
    else:
        prog = cache[key] = regex.compile(pat)
    return prog
Exemple #5
0
#! /usr/bin/env python1.5
Exemple #6
0
"""Regexp-based split and replace using the obsolete regex module.
Exemple #7
0
"""Regexp-based split and replace using the obsolete regex module.