
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What is the groups() method in regular expressions in Python?
The re.groups() method
This method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None. In later versions (from 1.5.1 on), a singleton tuple is returned in such cases.
example
>>> m = re.match(r"(\d+)\.(\d+)", "27.1835") >>> m.groups() ('27', '1835')
If we make the decimal place and everything after it optional, not all groups might participate in the match. These groups will default to None unless the default argument is given −
>>> m = re.match(r"(\d+)\.?(\d+)?", "27") >>> m.groups() # Second group defaults to None. ('27', None) >>> m.groups('0') # Now, the second group defaults to '0'. ('27', '0')
- Related Articles
- Working with simple groups in Java Regular Expressions
- Non capturing groups Java regular expressions:
- Named captured groups Java regular expressions
- Named capture groups JavaScript Regular Expressions
- Pattern.matches() method in Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions
- What is Regular Expressions?
- What is the difference between re.search() and re.findall() methods in Python regular expressions?
- Working with Matcher.start() method in Java Regular Expressions
- Working with Matcher.end() method in Java Regular Expressions
- Role of Matcher.matches() method in Java Regular Expressions
- Role of Matcher.group() method in Java Regular Expressions
- What are regular expressions in JavaScript?
- What are regular expressions in C#
- Role of Matcher.find(int) method in Java Regular Expressions

Advertisements