

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Named captured groups Java regular expressions
- Non capturing groups Java regular expressions:
- Named capture groups JavaScript Regular Expressions
- What is Regular Expressions?
- Working with simple groups in Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions
- Pattern.matches() method in Java Regular Expressions
- What is the role of brackets ([ ]) in JavaScript Regular Expressions?
- What is the difference between re.search() and re.findall() methods in Python regular expressions?
- What are regular expressions in JavaScript?
- What are regular expressions in C#
- What is the role of special characters in JavaScript Regular Expressions?
- Role of Matcher.matches() method in Java Regular Expressions
- Role of Matcher.group() method in Java Regular Expressions
- Working with Matcher.start() method in Java Regular Expressions
Advertisements