Python regex 模块,escape() 实例源码

我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用regex.escape()

项目:backrefs    作者:facelessuser    | 项目源码 | 文件源码
def test_octal_fail(self):
        """Test that octal fails properly."""

        pattern = regex.compile(b'Test')

        with pytest.raises(ValueError) as excinfo:
            bregex.compile_replace(pattern, br'\666')

        assert "octal escape value outside of range 0-0o377!" in str(excinfo.value)

        with pytest.raises(ValueError) as excinfo:
            bregex.compile_replace(pattern, br'\C\666\E')

        assert "octal escape value outside of range 0-0o377!" in str(excinfo.value)

        with pytest.raises(ValueError) as excinfo:
            bregex.compile_replace(pattern, br'\c\666')

        assert "octal escape value outside of range 0-0o377!" in str(excinfo.value)
项目:backrefs    作者:facelessuser    | 项目源码 | 文件源码
def quoted(self, i):
            r"""Handle quoted block."""

            quoted = []
            raw = []
            if not self.in_group(i.index - 1):
                try:
                    t = next(i)
                    while t != self._esc_end:
                        raw.append(t)
                        t = next(i)
                except StopIteration:
                    pass
                if len(raw):
                    quoted.extend([escape(self._empty.join(raw))])
            return quoted
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_bug_449964(self):
        # Fails for group followed by other escape.
        self.assertEqual(regex.sub(r'(?P<unk>x)', r'\g<1>\g<1>\b', 'xx'),
          "xx\bxx\b")
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_re_escape(self):
        p = ""
        self.assertEqual(regex.escape(p), p)
        for i in range(0, 256):
            p += chr(i)
            self.assertEqual(bool(regex.match(regex.escape(chr(i)), chr(i))),
              True)
            self.assertEqual(regex.match(regex.escape(chr(i)), chr(i)).span(),
              (0, 1))

        pat = regex.compile(regex.escape(p))
        self.assertEqual(pat.match(p).span(), (0, 256))
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_bug_612074(self):
        pat = u"[" + regex.escape(u"\u2039") + u"]"
        self.assertEqual(regex.compile(pat) and 1, 1)
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def test_replacement(self):
        self.assertEqual(regex.sub("test\?", "result\?\.\a\q\m\n", "test?"),
          "result\?\.\a\q\m\n")
        self.assertEqual(regex.sub(r"test\?", "result\?\.\a\q\m\n", "test?"),
          "result\?\.\a\q\m\n")

        self.assertEqual(regex.sub('(.)', r"\1\1", 'x'), 'xx')
        self.assertEqual(regex.sub('(.)', regex.escape(r"\1\1"), 'x'), r"\1\1")
        self.assertEqual(regex.sub('(.)', r"\\1\\1", 'x'), r"\1\1")
        self.assertEqual(regex.sub('(.)', lambda m: r"\1\1", 'x'), r"\1\1")
项目:oa_qian    作者:sunqb    | 项目源码 | 文件源码
def calc_unwanted_chars_re(self):
        unwanted_chars_re = u'[^\p{{AlNum}}{safe_chars}]+'.format(safe_chars=re.escape(self._safe_chars or ''))
        self.unwanted_chars_re = re.compile(unwanted_chars_re, re.IGNORECASE)

        if self._stop_words:
            unwanted_chars_and_words_re = unwanted_chars_re + u'|(?<!\p{AlNum})(?:\L<stop_words>)(?!\p{AlNum})'
            self.unwanted_chars_and_words_re = re.compile(unwanted_chars_and_words_re, re.IGNORECASE, stop_words=self._stop_words)
        else:
            self.unwanted_chars_and_words_re = None
项目:backrefs    作者:facelessuser    | 项目源码 | 文件源码
def test_quote(self):
        """Test quoting/escaping."""

        result = bregex.RegexSearchTemplate(r'Testing \Q(\s+[quote]*\s+)?\E!').apply()
        self.assertEqual(r'Testing %s!' % regex.escape(r'(\s+[quote]*\s+)?'), result)
项目:backrefs    作者:facelessuser    | 项目源码 | 文件源码
def test_quote_no_end(self):
        r"""Test quote where no \E is defined."""

        result = bregex.RegexSearchTemplate(r'Testing \Q(quote) with no [end]!').apply()
        self.assertEqual(r'Testing %s' % regex.escape(r'(quote) with no [end]!'), result)
项目:apache-scalp    作者:BalloonPlanet    | 项目源码 | 文件源码
def fill_replace_dict():
    global d_replace, re_replace
    # very first control-chars
    for i in range(0,20):
        d_replace["%%%x" % i] = "%00"
        d_replace["%%%X" % i] = "%00"
    # javascript charcode
    for i in range(33,127):
        c = "%c" % i
        d_replace["\\%o" % i] = c
        d_replace["\\%x" % i] = c
        d_replace["\\%X" % i] = c
        d_replace["0x%x" % i] = c
        d_replace["&#%d;" % i] = c
        d_replace["&#%x;" % i] = c
        d_replace["&#%X;" % i] = c
    # SQL words?
    d_replace["is null"]="=0"
    d_replace["like null"]="=0"
    d_replace["utc_time"]=""
    d_replace["null"]=""
    d_replace["true"]=""
    d_replace["false"]=""
    d_replace["localtime"]=""
    d_replace["stamp"]=""
    d_replace["binary"]=""
    d_replace["ascii"]=""
    d_replace["soundex"]=""
    d_replace["md5"]=""
    d_replace["between"]="="
    d_replace["is"]="="
    d_replace["not in"]="="
    d_replace["xor"]="="
    d_replace["rlike"]="="
    d_replace["regexp"]="="
    d_replace["sounds like"]="="
    re_replace = re.compile("(%s)" % "|".join(map(re.escape, d_replace.keys())))
项目:panphon    作者:dmort27    | 项目源码 | 文件源码
def read_xsampa_table(self):
        filename = os.path.join('data', 'ipa-xsampa.csv')
        filename = pkg_resources.resource_filename(__name__, filename)
        with open(filename, 'rb') as f:
            xs2ipa = {x[1]: x[0] for x in csv.reader(f, encoding='utf-8')}
        xs = sorted(xs2ipa.keys(), key=len, reverse=True)
        xs_regex = re.compile('|'.join(map(re.escape, xs)))
        return xs_regex, xs2ipa
项目:backrefs    作者:facelessuser    | 项目源码 | 文件源码
def parse_template(self, pattern):
            """Parse template."""

            i = ReplaceTokens(self._original, use_format=self.use_format)
            iter(i)
            self.result = [self._empty]

            for t in i:
                if len(t) > 1:
                    if self.use_format and t[0:1] == self._lc_bracket:
                        self.handle_format_group(t[1:-1].strip())
                    else:
                        c = t[1:]
                        if c[0:1].isdigit() and (self.use_format or len(c) == 3):
                            value = int(c, 8)
                            if value > 0xFF:
                                if self.binary:
                                    # Re fails on octal greater than 0o377 or 0xFF
                                    raise ValueError("octal escape value outside of range 0-0o377!")
                                self.result.append('\\u%04x' % value)
                            else:
                                self.result.append(self.string_convert('\\%03o' % value))
                        elif not self.use_format and (c[0:1].isdigit() or c[0:1] == self._group):
                            self.handle_group(t)
                        elif c == self._lc:
                            self.single_case(i, _LOWER)
                        elif c == self._lc_span:
                            self.span_case(i, _LOWER)
                        elif c == self._uc:
                            self.single_case(i, _UPPER)
                        elif c == self._uc_span:
                            self.span_case(i, _UPPER)
                        elif c == self._end:
                            # This is here just as a reminder that \E is ignored
                            pass
                        else:
                            self.result.append(t)
                else:
                    self.result.append(t)

            if len(self.result) > 1:
                self.literal_slots.append(self._empty.join(self.result))
                del self.result[:]
                self.result.append(self._empty)
                self.slot += 1

            self._template = self._empty.join(self.literal_slots)
            self.groups, self.literals = self.regex_parse_template(self._template, pattern)