Exemple #1
0
 def test_match_str(self):
     ''' test that we can match binary things in the str type '''
     r = re2.compile('(\\x09)')
     m = r.match('\x09')
     self.assertIsNotNone(m)
     g = m.groups()
     self.assertTrue(isinstance(g, tuple))
     self.assertTrue(isinstance(g[0], str))
     self.assertEqual('\x09', g[0])
Exemple #2
0
 def test_findall(self):
     s = """date 0 : 14/9/2000 date 1 : 20/04/1971 """
     reg = re2.compile(
         "([0-3]?[0-9]/[0-1]?[0-9]/([0-2][0-9])?[0-9][0-9])[^\\d]")
     res = reg.search(s, 0)
     self.assertEqual(res.groups(), ('14/9/2000', '20'))
     self.assertEqual(res.pos, 0)
     self.assertEqual(res.endpos, 39)
     self.assertEqual(res.span(), (9, 19))
     fall = re2.findall(reg, s)
     self.assertEqual(fall, [('14/9/2000', '20'), ('20/04/1971', '19')])
     fall = re2.findall(
         "([0-3]?[0-9]/[0-1]?[0-9]/([0-2][0-9])?[0-9][0-9])[^\\d]", s)
     self.assertEqual(fall, [('14/9/2000', '20'), ('20/04/1971', '19')])
Exemple #3
0
 def test_match_bytes(self):
     ''' test that we can match things in the bytes type '''
     r = re2.compile('(\\x09)')
     m = r.match(b'\x09')
     self.assertIsNotNone(m)
     g = m.groups()
     self.assertTrue(isinstance(g, tuple))
     self.assertTrue(isinstance(g[0], bytes))
     self.assertEqual(b'\x09', g[0])
     self.assertEqual(m.pos, 0)
     self.assertEqual(m.endpos, 1)
     self.assertEqual(m.string, b'\x09')
     self.assertIsNotNone(m.re)
     self.assertEqual(m.re.pattern, '(\\x09)')
     self.assertEqual(m.re.groups, 1)
     self.assertEqual(m.re.groupindex, {})
Exemple #4
0
 def test_compiled_match(self):
     r = re2.compile('ab([cde]fg)')
     m = r.match('abdfghij')
     self.assertIsNotNone(m)
     self.assertEqual(m.start(), 0)
     self.assertEqual(m.end(), 5)
     self.assertEqual(m.span(), (0, 5))
     self.assertEqual(m.groups(), ('dfg', ))
     self.assertEqual(m.groupdict(), {})
     self.assertEqual(m.pos, 0)
     self.assertEqual(m.endpos, 8)
     self.assertEqual(m.string, 'abdfghij')
     self.assertIsNotNone(m.re)
     self.assertEqual(m.re.pattern, 'ab([cde]fg)')
     self.assertEqual(m.re.groups, 1)
     self.assertEqual(m.re.groupindex, {})
 def __init__(self, dim=None, **opts):
     BenchPerfTest.__init__(self, **opts)
     pattern = "([0-9][0-9]?)/([0-9][0-9]?)/([0-9][0-9]([0-9][0-9])?)"
     self.exp1 = re.compile(pattern)
     self.exp2 = re2.compile(pattern)
Exemple #6
0
 def test_match_raise(self):
     '''test that using the API incorrectly fails'''
     r = re2.compile('ab([cde]fg)')
     self.assertRaise(lambda: re2.match(r, 'abdfghij'), TypeError)
Exemple #7
0
 def test_span_type(self):
     ''' verify that start/end return the native literal integer type '''
     r = re2.compile('abc')
     m = r.match('abc')
     self.assertTrue(isinstance(m.start(), type(1)))
     self.assertTrue(isinstance(m.end(), type(1)))
Exemple #8
0
 def test_invalid_pattern(self):
     ''' Verify that bad patterns raise an exception '''
     self.assertRaise(lambda: re2.compile(')'), Exception)
Exemple #9
0
 def test_match_bad_utf8_bytes(self):
     ''' Validate that we just return None on invalid utf-8 '''
     r = re2.compile('\\x80')
     m = r.match(b'\x80')
     self.assertIsNone(m)