6 examples of 'python regex capture group' in Python

Every line of 'python regex capture group' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
109def re_group(r):
110 if dialect == 'vim':
111 return r'%(' + r + r')'
112 return r'(' + r + r')'
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

75def search_group(self, str, group, pos=0):
76 """Search a string for a pattern.
77
78 If the pattern was not found, then None is returned,
79 otherwise, the location where the pattern was found,
80 as well as any specified group are returned.
81 """
82 self.__a()
83 try:
84 r=self._r
85 l=r.search(str, pos)
86 if l < 0: return None
87 return l, r.group(*group)
88 finally: self.__r()
27def __group0(self, arg):
28 return ''
290def get_regexp_matches(self, string, pattern, *groups):
291 """Returns a list of all non-overlapping matches in the given string.
292
293 ``string`` is the string to find matches from and ``pattern`` is the
294 regular expression. See `BuiltIn.Should Match Regexp` for more
295 information about Python regular expression syntax in general and how
296 to use it in Robot Framework test data in particular.
297
298 If no groups are used, the returned list contains full matches. If one
299 group is used, the list contains only contents of that group. If
300 multiple groups are used, the list contains tuples that contain
301 individual group contents. All groups can be given as indexes (starting
302 from 1) and named groups also as names.
303
304 Examples:
305 | ${no match} = | Get Regexp Matches | the string | xxx |
306 | ${matches} = | Get Regexp Matches | the string | t.. |
307 | ${one group} = | Get Regexp Matches | the string | t(..) | 1 |
308 | ${named group} = | Get Regexp Matches | the string | t(?P<name>..) | name |
309 | ${two groups} = | Get Regexp Matches | the string | t(.)(.) | 1 | 2 |
310 =>
311 | ${no match} = []
312 | ${matches} = ['the', 'tri']
313 | ${one group} = ['he', 'ri']
314 | ${named group} = ['he', 'ri']
315 | ${two groups} = [('h', 'e'), ('r', 'i')]
316
317 New in Robot Framework 2.9.
318 """
319 regexp = re.compile(pattern)
320 groups = [self._parse_group(g) for g in groups]
321 return [m.group(*groups) for m in regexp.finditer(string)]
109def matches(self, regex, group=None):
110 """ like py re.finditer """
111 for m in _R(regex).sre.finditer(self):
112 if group is None: yield wrap_match(m)
113 else: yield wrap_match(m)[group]
1509def testFindOuterCaptureGroups(self):
1510 r = re.compile('^(([A-Z]+) [1-5])([^\W_]+)$', re.U)
1511 m = re.match(r, 'THIS 3aéAB')
1512 self.assertEqual(is_outer_group(m, 1), True)
1513 self.assertEqual(is_outer_group(m, 2), False)
1514 self.assertEqual(is_outer_group(m, 3), True)
1515
1516 f = group_map_function(m, 2)
1517 self.assertEqual(f(1), 1)
1518 self.assertEqual(f(2), 3)
1519 self.assertRaises(KeyError, f, 3)
1520
1521 r = re.compile('^aaa([A-Z]+)bb$', re.U)
1522 m = re.match(r, 'aaaBBbb')
1523 f = group_map_function(m, 2)
1524 self.assertEqual(f(1), 1)
1525 self.assertRaises(KeyError, f, 2)
1526
1527 regex = '@ (a(b)c) d (e(f(g)h(i(j)k)l)m) n (o) p (q(r)s) t'
1528 r = re.compile(regex, re.U)
1529 m = re.match(r, '@ abc d efghijklm n o p qrs t')
1530 f = group_map_function(m, 4)
1531 self.assertEqual(f(1), 1)
1532 self.assertEqual(f(2), 3)
1533 self.assertEqual(f(3), 8)
1534 self.assertEqual(f(4), 9)
1535 self.assertRaises(KeyError, f, 5)

Related snippets