Python re._expand() Examples

The following are code examples for showing how to use re._expand(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: web_ctp   Author: molebot   File: _sre.py    (license) View Source Project 5 votes vote down vote up
def expand(self, template):
        """Return the string obtained by doing backslash substitution and
        resolving group references on template."""
        import sre
        return sre._expand(self.re, self, template) 
Example 2
Project: ouroboros   Author: pybee   File: _sre.py    (license) View Source Project 5 votes vote down vote up
def expand(self, template):
        """Return the string obtained by doing backslash substitution and
        resolving group references on template."""
        import sre
        return sre._expand(self.re, self, template) 
Example 3
Project: PythonSed   Author: GillesArcas   File: sed.py    (license) View Source Project 4 votes vote down vote up
def re_sub_ex(pattern, compiled, replacement, string, count, flags):
    # re.sub() extended:
    # - an unmatched group returns an empty string rather than None
    #   (http://gromgull.net/blog/2012/10/python-regex-unicode-and-brokenness/)
    # - the nth occurrence is replaced rather than the nth first ones
    #   (https://mail.python.org/pipermail/python-list/2008-December/475132.html)

    class Match():
        def __init__(self, m):
            self.m = m
            self.string = m.string
        def group(self, n):
            return self.m.group(n) or ''

    class Nth(object):
        def __init__(self):
            self.calls = 0
        def __call__(self, matchobj):
            if count == 0:
                return re._expand(pattern, Match(matchobj), replacement)
            else:
                self.calls += 1
                if self.calls == count:
                    return re._expand(pattern, Match(matchobj), replacement)
                else:
                    return matchobj.group(0)

    try:
        if compiled is None:
            string_res, nsubst = re.subn(pattern, Nth(), string, count, flags)
        else:
            string_res, nsubst = compiled.subn(Nth(), string, count)

    except re.error as e:
        raise SedException('regexp: %s' % e.message)
    except:
        raise

    # nsubst is the number of subst which would have been made without
    # the redefinition
    if count == 0:
        return (nsubst > 0), string_res
    else:
        return (nsubst >= count), string_res


# -- Main -------------------------------------------------------------------- 
Example 4
Project: Meiji   Author: GiovanniBalestrieri   File: operators.py    (license) View Source Project 4 votes vote down vote up
def Builtin_REPLACE(expr, ctx):
    """
    http://www.w3.org/TR/sparql11-query/#func-substr
    """
    text = string(expr.arg)
    pattern = string(expr.pattern)
    replacement = string(expr.replacement)
    flags = expr.flags

    # python uses \1, xpath/sparql uses $1
    replacement = re.sub('\\$([0-9]*)', r'\\\1', replacement)

    def _r(m):

        # Now this is ugly.
        # Python has a "feature" where unmatched groups return None
        # then re.sub chokes on this.
        # see http://bugs.python.org/issue1519638 , fixed and errs in py3.5

        # this works around and hooks into the internal of the re module...

        # the match object is replaced with a wrapper that
        # returns "" instead of None for unmatched groups

        class _m():
            def __init__(self, m):
                self.m = m
                self.string = m.string

            def group(self, n):
                return m.group(n) or ""

        return re._expand(pattern, _m(m), replacement)

    cFlag = 0
    if flags:
        # Maps XPath REGEX flags (http://www.w3.org/TR/xpath-functions/#flags)
        # to Python's re flags
        flagMap = dict(
            [('i', re.IGNORECASE), ('s', re.DOTALL), ('m', re.MULTILINE)])
        cFlag = reduce(pyop.or_, [flagMap.get(f, 0) for f in flags])

        # @@[email protected]@ either datatype OR lang, NOT both

    # this is necessary due to different treatment of unmatched groups in
    # python versions. see comments above in _r(m).
    compat_r = unicode(replacement) if sys.version_info[:2] >= (3, 5) else _r

    return Literal(re.sub(unicode(pattern), compat_r, text, cFlag),
                   datatype=text.datatype, lang=text.language) 
Example 5
Project: prophet   Author: MKLab-ITI   File: operators.py    (license) View Source Project 4 votes vote down vote up
def Builtin_REPLACE(expr, ctx):
    """
    http://www.w3.org/TR/sparql11-query/#func-substr
    """
    text = string(expr.arg)
    pattern = string(expr.pattern)
    replacement = string(expr.replacement)
    flags = expr.flags

    # python uses \1, xpath/sparql uses $1
    replacement = re.sub('\\$([0-9]*)', r'\\\1', replacement)

    def _r(m):

        # Now this is ugly.
        # Python has a "feature" where unmatched groups return None
        # then re.sub chokes on this.
        # see http://bugs.python.org/issue1519638 , fixed and errs in py3.5

        # this works around and hooks into the internal of the re module...

        # the match object is replaced with a wrapper that
        # returns "" instead of None for unmatched groups

        class _m():
            def __init__(self, m):
                self.m = m
                self.string = m.string

            def group(self, n):
                return m.group(n) or ""

        return re._expand(pattern, _m(m), replacement)

    cFlag = 0
    if flags:
        # Maps XPath REGEX flags (http://www.w3.org/TR/xpath-functions/#flags)
        # to Python's re flags
        flagMap = dict(
            [('i', re.IGNORECASE), ('s', re.DOTALL), ('m', re.MULTILINE)])
        cFlag = reduce(pyop.or_, [flagMap.get(f, 0) for f in flags])

        # @@[email protected]@ either datatype OR lang, NOT both

    # this is necessary due to different treatment of unmatched groups in
    # python versions. see comments above in _r(m).
    compat_r = unicode(replacement) if sys.version_info[:2] >= (3, 5) else _r

    return Literal(re.sub(unicode(pattern), compat_r, text, cFlag),
                   datatype=text.datatype, lang=text.language)