Exemple #1
0
def getlkpitem(items):
    match_rgx1 = "\s*[\'\"](.+?)[\'\"]\s*:\s*[\'\"](.*)[\'\"]\s*,\s*(.*)"
    match_rgx2 = "\s*[\'\"](.+?)[\'\"]\s*:\s*[\'\"](.*)[\'\"]\s*"
    matched = match(match_rgx, items)
    if not matched:
        matched = match(match_rgx2, items)
    return matched
Exemple #2
0
 def test_bigcharset(self):
     self.assertEqual(re.match(u"([\u2222\u2223])",
                               u"\u2222").group(1), u"\u2222")
     self.assertEqual(re.match(u"([\u2222\u2223])",
                               u"\u2222", re.UNICODE).group(1), u"\u2222")
     r = u'[%s]' % u''.join(map(unichr, range(256, 2**16, 255)))
     self.assertEqual(re.match(r, u"\uff01", re.UNICODE).group(), u"\uff01")
Exemple #3
0
 def test_bug_527371(self):
     # bug described in patches 527371/672491
     self.assertEqual(re.match(r'(a)?a','a').lastindex, None)
     self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1)
     self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a')
     self.assertEqual(re.match("(?P<a>a(b))", "ab").lastgroup, 'a')
     self.assertEqual(re.match("((a))", "a").lastindex, 1)
Exemple #4
0
 def test_string_boundaries(self):
     # See http://bugs.python.org/issue10713
     self.assertEqual(re.search(r"\b(abc)\b", "abc").group(1),
                      "abc")
     # There's a word boundary at the start of a string.
     self.assertTrue(re.match(r"\b", "abc"))
     # A non-empty string includes a non-boundary zero-length match.
     self.assertTrue(re.search(r"\B", "abc"))
     # There is no non-boundary match at the start of a string.
     self.assertFalse(re.match(r"\B", "abc"))
     # However, an empty string contains no word boundaries, and also no
     # non-boundaries.
     self.assertEqual(re.search(r"\B", ""), None)
     # This one is questionable and different from the perlre behaviour,
     # but describes current behavior.
     self.assertEqual(re.search(r"\b", ""), None)
     # A single word-character string has two boundaries, but no
     # non-boundary gaps.
     self.assertEqual(len(re.findall(r"\b", "a")), 2)
     self.assertEqual(len(re.findall(r"\B", "a")), 0)
     # If there are no words, there are no boundaries
     self.assertEqual(len(re.findall(r"\b", " ")), 0)
     self.assertEqual(len(re.findall(r"\b", "   ")), 0)
     # Can match around the whitespace.
     self.assertEqual(len(re.findall(r"\B", " ")), 2)
Exemple #5
0
 def apply(self, pos, tokens):
     if pos + self.offset < 0:
         return False, []
     if pos + self.offset >= len(tokens):
         return False, []
     if self.rgxname not in self.norm.rgxs:
         return False, []
     if self.src == 'lcase':
         val = tokens[pos + self.offset].get('tknorm')
         if val is None:
             return False, []
         matched = match(self.norm.rgxs[self.rgxname], val)
         if matched:
             if not len(matched.groups()):
                 return True, [matched.group(0)]
             else:
                 return True, matched.groups()
         else:
             return False, []
     else:
         if tokens[pos + self.offset].text:
             matched = match(self.norm.rgxs[self.rgxname],
                             (tokens[pos + self.offset].text).strip())
         else:
             return False, []
         if matched:
             if not len(matched.groups()):
                 return True, [matched.group(0)]
             else:
                 return True, matched.groups()
         else:
             return False, []
Exemple #6
0
 def test_bug_448951(self):
     # bug 448951 (similar to 429357, but with single char match)
     # (Also test greedy matches.)
     for op in '','?','*':
         self.assertEqual(re.match(r'((.%s):)?z'%op, 'z').groups(),
                          (None, None))
         self.assertEqual(re.match(r'((.%s):)?z'%op, 'a:z').groups(),
                          ('a:', 'a'))
Exemple #7
0
    def test_getlower(self):
        # PCRE: disabled, we're not testing _sre here
        #import _sre
        #self.assertEqual(_sre.getlower(ord('A'), 0), ord('a'))
        #self.assertEqual(_sre.getlower(ord('A'), re.LOCALE), ord('a'))
        #self.assertEqual(_sre.getlower(ord('A'), re.UNICODE), ord('a'))

        self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
        self.assertEqual(re.match("abc", u"ABC", re.I).group(0), "ABC")
Exemple #8
0
 def test_search_star_plus(self):
     self.assertEqual(re.search('x*', 'axx').span(0), (0, 0))
     self.assertEqual(re.search('x*', 'axx').span(), (0, 0))
     self.assertEqual(re.search('x+', 'axx').span(0), (1, 3))
     self.assertEqual(re.search('x+', 'axx').span(), (1, 3))
     self.assertEqual(re.search('x', 'aaa'), None)
     self.assertEqual(re.match('a*', 'xxx').span(0), (0, 0))
     self.assertEqual(re.match('a*', 'xxx').span(), (0, 0))
     self.assertEqual(re.match('x*', 'xxxa').span(0), (0, 3))
     self.assertEqual(re.match('x*', 'xxxa').span(), (0, 3))
     self.assertEqual(re.match('a+', 'xxx'), None)
Exemple #9
0
 def test_ignore_case(self):
     self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
     self.assertEqual(re.match("abc", u"ABC", re.I).group(0), "ABC")
     self.assertEqual(re.match(r"(a\s[^a])", "a b", re.I).group(1), "a b")
     self.assertEqual(re.match(r"(a\s[^a]*)", "a bb", re.I).group(1), "a bb")
     self.assertEqual(re.match(r"(a\s[abc])", "a b", re.I).group(1), "a b")
     self.assertEqual(re.match(r"(a\s[abc]*)", "a bb", re.I).group(1), "a bb")
     self.assertEqual(re.match(r"((a)\s\2)", "a a", re.I).group(1), "a a")
     self.assertEqual(re.match(r"((a)\s\2*)", "a aa", re.I).group(1), "a aa")
     self.assertEqual(re.match(r"((a)\s(abc|a))", "a a", re.I).group(1), "a a")
     self.assertEqual(re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1), "a aa")
Exemple #10
0
 def test_bug_418626(self):
     # bugs 418626 at al. -- Testing Greg Chapman's addition of op code
     # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
     # pattern '*?' on a long string.
     self.assertEqual(re.match('.*?c', 10000*'ab'+'cd').end(0), 20001)
     self.assertEqual(re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0),
                      20003)
     self.assertEqual(re.match('.*?cd', 20000*'abc'+'de').end(0), 60001)
     # non-simple '*?' still used to hit the recursion limit, before the
     # non-recursive scheme was implemented.
     self.assertEqual(re.search('(a|b)*?c', 10000*'ab'+'cd').end(0), 20001)
Exemple #11
0
 def test_repeat_minmax_overflow_maxrepeat(self):
     try:
         # PCRE: this is defined in pcre module
         #from _sre import MAXREPEAT
         from pcre import MAXREPEAT
     except ImportError:
         self.skipTest('requires _sre.MAXREPEAT constant')
     string = "x" * 100000
     self.assertIsNone(re.match(r".{%d}" % (MAXREPEAT - 1), string))
     self.assertEqual(re.match(r".{,%d}" % (MAXREPEAT - 1), string).span(),
                      (0, 100000))
     self.assertIsNone(re.match(r".{%d,}?" % (MAXREPEAT - 1), string))
     self.assertRaises(OverflowError, re.compile, r".{%d}" % MAXREPEAT)
     self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT)
     self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT)
Exemple #12
0
def check_whitelist(rule, nxlog):
    """

    :param  dict rule:
    :param dict nxlog:
    :return bool: Did the `rule` whitelisted the `nxlog`?
    """
    negative_id = any(i < 0 for i in rule['wl'])
    for mz in rule['mz']:
        for nb in itertools.count():
            matched = False
            nxlog_zone = nxlog.get('zone%d' % nb, '')
            if not nxlog_zone:
                break
            if mz.startswith('$'):  # named argument
                mz_zone, mz_var = mz.split(':', 1)
                if mz_zone.endswith('_X'):  # regexp named argument
                    if pcre.match(mz_var, nxlog['var_name%d' % nb], pcre.I) and nxlog_zone == mz_zone[1:-6]:
                        matched = True
                elif nxlog['var_name%d' % nb] == mz_var and nxlog_zone == mz_zone[1:-4]:
                    matched = True
            elif nxlog_zone in mz:  # zone without argument
                matched = True

            if not matched:  # We didn't manage to match the nxlog zone `nxlog_zone` with anything in our `rule`
                return False

            if negative_id:
                if int(nxlog['id%d' % nb]) in rule['wl']:
                    return False
            elif int(nxlog['id%d' % nb]) not in rule['wl']:
                return False
    return True
 def test_groupdict(self):
     self.assertEqual(
         re.match('(?P<first>first) (?P<second>second)',
                  'first second').groupdict(), {
                      'first': 'first',
                      'second': 'second'
                  })
Exemple #14
0
    def run(self):
        if not HAVE_PCRE:
            return False

        ret = False
        pcres = list()
        kits = [
            "angler",
            "dotcachef",
            "fiesta",
            "goon",
            "magnitude",
            "neutrino",
            "nuclear",
            "orange",
            "rig",
        ]
        office = [
            "doc",
            "xls",
            "ppt",
        ]
        if "feeds" in self.results and self.results["feeds"]:
            if "Punch_Plus_Plus_PCREs" in self.results["feeds"]:
                with open(self.results["feeds"]["Punch_Plus_Plus_PCREs"], "r") as feedfile:
                    data = feedfile.read().splitlines()
                if data:
                    for item in data:
                        regex = item.split()[0]
                        desc = " ".join(item.split()[1:])
                        pcres.append((regex, desc))

        if "network" in self.results and self.results["network"]:
            if "http" in self.results["network"] and self.results["network"]["http"]:
                for req in self.results["network"]["http"]:
                    for regex in pcres:
                        if pcre.match(regex[0], req["uri"]):
                            ret = True
                            add = {"URL": req["uri"]}
                            if add not in self.data:
                                self.data.append(add)
                            self.data.append({"Desc": regex[1]})
                            self.data.append({"PCRE": regex[0]})
                            for ek in kits:
                                check1 = " {0} ek".format(ek)
                                check2 = " {0} exploit kit".format(ek)
                                desc = regex[1].lower()
                                if check1 in desc or check2 in desc:
                                    if ek not in self.families:
                                        self.families = [ek]
                                if self.results["info"]["package"] in office:
                                    if "dridex" in regex[1].lower() and "dridex" not in self.families:
                                        if not self.families:
                                            self.families = ["dridex"]

        if ret:
            return True

        return False
Exemple #15
0
def extract(fingerprint):
    pattern_compiled = pcre.compile(REGEX, flags=parse_compile_flags(RESULT['rule']['pattern']['flags']))
    matched = pcre.match(pattern_compiled, fingerprint)
    if not matched:
        return None
    result = copy.deepcopy(RESULT)
    result['rule']['portinfo'] = substitute_portinfo_template(result['rule']['portinfo'], matched.groups())
    return result
Exemple #16
0
    def test_bug_6561(self):
        # '\d' should match characters in Unicode category 'Nd'
        # (Number, Decimal Digit), but not those in 'Nl' (Number,
        # Letter) or 'No' (Number, Other).
        decimal_digits = [
            u'\u0037', # '\N{DIGIT SEVEN}', category 'Nd'
            u'\u0e58', # '\N{THAI DIGIT SIX}', category 'Nd'
            u'\uff10', # '\N{FULLWIDTH DIGIT ZERO}', category 'Nd'
            ]
        for x in decimal_digits:
            self.assertEqual(re.match('^\d$', x, re.UNICODE).group(0), x)

        not_decimal_digits = [
            u'\u2165', # '\N{ROMAN NUMERAL SIX}', category 'Nl'
            u'\u3039', # '\N{HANGZHOU NUMERAL TWENTY}', category 'Nl'
            u'\u2082', # '\N{SUBSCRIPT TWO}', category 'No'
            u'\u32b4', # '\N{CIRCLED NUMBER THIRTY NINE}', category 'No'
            ]
        for x in not_decimal_digits:
            self.assertIsNone(re.match('^\d$', x, re.UNICODE))
Exemple #17
0
    def test_re_match(self):
        self.assertEqual(re.match('a', 'a').groups(), ())
        self.assertEqual(re.match('(a)', 'a').groups(), ('a',))
        self.assertEqual(re.match(r'(a)', 'a').group(0), 'a')
        self.assertEqual(re.match(r'(a)', 'a').group(1), 'a')
        self.assertEqual(re.match(r'(a)', 'a').group(1, 1), ('a', 'a'))

        pat = re.compile('((a)|(b))(c)?')
        self.assertEqual(pat.match('a').groups(), ('a', 'a', None, None))
        self.assertEqual(pat.match('b').groups(), ('b', None, 'b', None))
        self.assertEqual(pat.match('ac').groups(), ('a', 'a', None, 'c'))
        self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
        self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))

        # A single group
        m = re.match('(a)', 'a')
        self.assertEqual(m.group(0), 'a')
        self.assertEqual(m.group(0), 'a')
        self.assertEqual(m.group(1), 'a')
        self.assertEqual(m.group(1, 1), ('a', 'a'))

        pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
        self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
        self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
                         (None, 'b', None))
        self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
 def test_bug_725106(self):
     # capturing groups in alternatives in repeats
     self.assertEqual(re.match('^((a)|b)*', 'abc').groups(), ('b', 'a'))
     self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(), ('c', 'b'))
     self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(), ('b', None))
     self.assertEqual(
         re.match('^((a)c|[ab])*', 'abc').groups(), ('b', None))
     self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(), ('b', 'a'))
     self.assertEqual(
         re.match('^(([ab])|c)*?d', 'abcd').groups(), ('c', 'b'))
     self.assertEqual(
         re.match('^((d)|[ab])*?c', 'abc').groups(), ('b', None))
     self.assertEqual(
         re.match('^((a)c|[ab])*?c', 'abc').groups(), ('b', None))
Exemple #19
0
 def test_unlimited_zero_width_repeat(self):
     # Issue #9669
     self.assertIsNone(re.match(r'(?:a?)*y', 'z'))
     self.assertIsNone(re.match(r'(?:a?)+y', 'z'))
     self.assertIsNone(re.match(r'(?:a?){2,}y', 'z'))
     self.assertIsNone(re.match(r'(?:a?)*?y', 'z'))
     self.assertIsNone(re.match(r'(?:a?)+?y', 'z'))
     self.assertIsNone(re.match(r'(?:a?){2,}?y', 'z'))
Exemple #20
0
 def loads(cls, input_str: str):
     null = None
     pcre.enable_re_template_mode()
     json = pcre.match(cls.__pattern, input_str)
     if input_str.isdigit() or (input_str.startswith('-') and input_str[1:].isdigit()):
         return input_str
     if json is None:
         if input_str.startswith('"') and input_str.endswith('"'):
             return leval(input_str)
         raise SyntaxError('Invalid json format')
     else:
         json = json.group(0)
         res: dict = eval(json, {}, {'null': null})
     return res
Exemple #21
0
 def test_sre_character_literals(self):
     for i in [0, 8, 16, 32, 64, 127, 128, 255]:
         self.assertNotEqual(re.match(r"\%03o" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"\%03o0" % i, chr(i)+"0"), None)
         self.assertNotEqual(re.match(r"\%03o8" % i, chr(i)+"8"), None)
         self.assertNotEqual(re.match(r"\x%02x" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"\x%02x0" % i, chr(i)+"0"), None)
         self.assertNotEqual(re.match(r"\x%02xz" % i, chr(i)+"z"), None)
     self.assertRaises(re.error, re.match, "\911", "")
Exemple #22
0
 def test_sre_character_class_literals(self):
     for i in [0, 8, 16, 32, 64, 127, 128, 255]:
         self.assertNotEqual(re.match(r"[\%03o]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\%03o0]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\%03o8]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\x%02x]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\x%02x0]" % i, chr(i)), None)
         self.assertNotEqual(re.match(r"[\x%02xz]" % i, chr(i)), None)
     self.assertRaises(re.error, re.match, "[\911]", "")
 def test_bug_725106(self):
     # capturing groups in alternatives in repeats
     self.assertEqual(re.match('^((a)|b)*', 'abc').groups(),
                      ('b', 'a'))
     self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(),
                      ('c', 'b'))
     self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(),
                      ('b', None))
     self.assertEqual(re.match('^((a)c|[ab])*', 'abc').groups(),
                      ('b', None))
     self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(),
                      ('b', 'a'))
     self.assertEqual(re.match('^(([ab])|c)*?d', 'abcd').groups(),
                      ('c', 'b'))
     self.assertEqual(re.match('^((d)|[ab])*?c', 'abc').groups(),
                      ('b', None))
     self.assertEqual(re.match('^((a)c|[ab])*?c', 'abc').groups(),
                      ('b', None))
Exemple #24
0
 def test_re_groupref(self):
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
                      ('|', 'a'))
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1?$', 'a').groups(),
                      (None, 'a'))
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', 'a|'), None)
     self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a'), None)
     self.assertEqual(re.match(r'^(?:(a)|c)(\1)$', 'aa').groups(),
                      ('a', 'a'))
     self.assertEqual(re.match(r'^(?:(a)|c)(\1)?$', 'c').groups(),
                      (None, None))
Exemple #25
0
    def runrulesets(self, tokens):
        if self.verboselvl:
            sys.stderr.write('\tRunning normaliser rules\n')
        for ridx, ruleset in enumerate(self.rulesequence):
            if self.verboselvl > 1:
                sys.stderr.write('\t\tRunning normaliser rule set {0} of {1}\n'.format(ridx+1, len(self.rulesequence)))
                if self.verboselvl > 2:
                    debugnosteps = 20
                    debugline = '\t\t\t0% [{0:' + str(debugnosteps) + '}] 100%\r'
                    debugoutline = ''
            for i, tk in enumerate(tokens):
                if self.verboselvl > 2:
                    if len(tokens)//debugnosteps:
                        if i % (len(tokens)//debugnosteps) == 0:
                            progress = i // (len(tokens)//debugnosteps)
                            debugoutline = debugline.format('*'*progress)
                            sys.stderr.write(debugoutline)
                            sys.stderr.flush()
                    else:
                        debugoutline = debugline.format('*'*debugnosteps)
                        sys.stderr.write(debugoutline)
                        sys.stderr.flush()


                norm_empty = (tk.get('norm') is None or
                              not match('^[a-z ]*$', tk.get('norm')) or
                              ruleset[:4] == 'ssml')
                if tk.tag == 'tk' and norm_empty:
                    for rule in self.rules[ruleset]:
                        tokensxml = []
                        for tk in tokens:
                            tokensxml.append(etree.tostring(tk,
                                                            pretty_print=True,
                                                            encoding='UTF-8')
                                             .strip())
                        matched = rule.apply(i, tokens)
                        if matched:
                            newtokensxml = []
                            for tk in tokens:
                                ntkstr = etree.tostring(tk,
                                                        pretty_print=True,
                                                        encoding='UTF-8')
                                newtokensxml.append(ntkstr.strip())
                            break
            if self.verboselvl > 2:
                sys.stderr.write('\n')
Exemple #26
0
def posnumbersonly(lkp, string, args, output):
    n = ''
    leading = True
    for i, c in enumerate(string):
        # minus handled by rule and should be tokenised separately
        # one result is single '-' in filter returns nothing.
        if i == 0 and c == '-':
            pass
            #output.append(lkp['-'])
        elif c == args['separator']:
            pass
        elif c == '0' and leading and args['leadingzeros'] == 't':
            output.append(lkp['0'])
        elif match('[0-9]', c):
            leading = False
            n = n + c
        else:
            break
    return n
Exemple #27
0
 def test_repeat_minmax_overflow(self):
     # Issue #13169
     string = "x" * 100000
     self.assertEqual(re.match(r".{65535}", string).span(), (0, 65535))
     self.assertEqual(re.match(r".{,65535}", string).span(), (0, 65535))
     self.assertEqual(re.match(r".{65535,}?", string).span(), (0, 65535))
     self.assertEqual(re.match(r".{65536}", string).span(), (0, 65536))
     self.assertEqual(re.match(r".{,65536}", string).span(), (0, 65536))
     self.assertEqual(re.match(r".{65536,}?", string).span(), (0, 65536))
     # 2**128 should be big enough to overflow both SRE_CODE and Py_ssize_t.
     self.assertRaises(OverflowError, re.compile, r".{%d}" % 2**128)
     self.assertRaises(OverflowError, re.compile, r".{,%d}" % 2**128)
     self.assertRaises(OverflowError, re.compile, r".{%d,}?" % 2**128)
     self.assertRaises(OverflowError, re.compile, r".{%d,%d}" % (2**129, 2**128))
    def test_re_groupref_exists(self):
        self.assertEqual(
            re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(), ('(', 'a'))
        self.assertEqual(
            re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(), (None, 'a'))
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'), None)
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a'), None)
        self.assertEqual(
            re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(), ('a', 'b'))
        self.assertEqual(
            re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups(), (None, 'd'))
        self.assertEqual(
            re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups(), (None, 'd'))
        self.assertEqual(
            re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups(), ('a', ''))

        # Tests for bug #1177831: exercise groups other than the first group
        p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
        self.assertEqual(p.match('abc').groups(), ('a', 'b', 'c'))
        self.assertEqual(p.match('ad').groups(), ('a', None, 'd'))
        self.assertEqual(p.match('abd'), None)
        self.assertEqual(p.match('ac'), None)
    def test_re_groupref_exists(self):
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
                         ('(', 'a'))
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(),
                         (None, 'a'))
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'), None)
        self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a'), None)
        self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
                         ('a', 'b'))
        self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
                         (None, 'd'))
        self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
                         (None, 'd'))
        self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups(),
                         ('a', ''))

        # Tests for bug #1177831: exercise groups other than the first group
        p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
        self.assertEqual(p.match('abc').groups(),
                         ('a', 'b', 'c'))
        self.assertEqual(p.match('ad').groups(),
                         ('a', None, 'd'))
        self.assertEqual(p.match('abd'), None)
        self.assertEqual(p.match('ac'), None)
Exemple #30
0
 def test_bug_725149(self):
     # mark_stack_base restoring before restoring marks
     self.assertEqual(re.match('(a)(?:(?=(b)*)c)*', 'abb').groups(),
                      ('a', None))
     self.assertEqual(re.match('(a)((?!(b)*))*', 'abb').groups(),
                      ('a', None, None))
 def test_getattr(self):
     self.assertEqual(re.match("(a)", "a").pos, 0)
     self.assertEqual(re.match("(a)", "a").endpos, 1)
     self.assertEqual(re.match("(a)", "a").string, "a")
     self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1)))
     self.assertNotEqual(re.match("(a)", "a").re, None)
Exemple #32
0
 def test_stack_overflow(self):
     # nasty cases that used to overflow the straightforward recursive
     # implementation of repeated groups.
     self.assertEqual(re.match('(x)*', 50000*'x').group(1), 'x')
     self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
     self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
Exemple #33
0
 def test_bug_113254(self):
     self.assertEqual(re.match(r'(a)|(b)', 'b').start(1), -1)
     self.assertEqual(re.match(r'(a)|(b)', 'b').end(1), -1)
     self.assertEqual(re.match(r'(a)|(b)', 'b').span(1), (-1, -1))
    def test_repeat_minmax(self):
        self.assertEqual(re.match("^(\w){1}$", "abc"), None)
        self.assertEqual(re.match("^(\w){1}?$", "abc"), None)
        self.assertEqual(re.match("^(\w){1,2}$", "abc"), None)
        self.assertEqual(re.match("^(\w){1,2}?$", "abc"), None)

        self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){3}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,3}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){1,4}?$", "abc").group(1), "c")
        self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")

        self.assertEqual(re.match("^x{1}$", "xxx"), None)
        self.assertEqual(re.match("^x{1}?$", "xxx"), None)
        self.assertEqual(re.match("^x{1,2}$", "xxx"), None)
        self.assertEqual(re.match("^x{1,2}?$", "xxx"), None)

        self.assertNotEqual(re.match("^x{3}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,3}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,4}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{3}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,3}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{1,4}?$", "xxx"), None)
        self.assertNotEqual(re.match("^x{3,4}?$", "xxx"), None)

        self.assertEqual(re.match("^x{}$", "xxx"), None)
        self.assertNotEqual(re.match("^x{}$", "x{}"), None)
 def test_anyall(self):
     self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0),
                      "a\nb")
     self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
                      "a\n\nb")
    def test_non_consuming(self):
        self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
        self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
        self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
        self.assertEqual(re.match("(a(?=\s[abc]*))", "a bc").group(1), "a")
        self.assertEqual(re.match(r"(a)(?=\s\1)", "a a").group(1), "a")
        self.assertEqual(re.match(r"(a)(?=\s\1*)", "a aa").group(1), "a")
        self.assertEqual(re.match(r"(a)(?=\s(abc|a))", "a a").group(1), "a")

        self.assertEqual(re.match(r"(a(?!\s[^a]))", "a a").group(1), "a")
        self.assertEqual(re.match(r"(a(?!\s[abc]))", "a d").group(1), "a")
        self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
        self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
Exemple #37
0
 def test_category(self):
     self.assertEqual(re.match(r"(\s)", " ").group(1), " ")
 def test_expand(self):
     self.assertEqual(re.match("(?P<first>first) (?P<second>second)",
                               "first second")
                               .expand(r"\2 \1 \g<second> \g<first>"),
                      "second first second first")