Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (16.4k points)
closed by
Can anyone tell me, What does this group() method in regular expressions means in python?

4 Answers

0 votes
by (19k points)
Best answer
To access the matched substrings using re.findall():

import re

string = "Hello, my name is John Doe"

matches = re.findall(r"\b\w+\b", string)

print(matches)  # Output: ['Hello', 'my', 'name', 'is', 'John', 'Doe']

In this concise version, the re.findall() function is used directly with the pattern \b\w+\b to extract all the words from the given string. The matched substrings are stored in the matches list, which is then printed.
0 votes
by (26.4k points)

For re details counsel this doc. For your situation: 

group(0) represents all coordinated with string, subsequently abc, that is 3 gatherings a, b and c 

group(i) represents i'th bunch, and referring to documentation

If a group matches multiple times, only the last match is accessible

thus group(1) represents the last match, c 

Your + is deciphered as gathering redundancy, in the event that you need to repeat [abc] inside the gathering, move + into brackets:

>>> re.match("([abc])", "abc").groups()


>>> re.match("([abc]+)", "abc").groups()


Wanna become a Python expert? Come and join the python certification course and get certified.

For more details, do check out the below video tutorial...

0 votes
by (25.7k points)
In Python's regular expressions module, the group() method is used to retrieve the matched substring from a regular expression search. When a regular expression pattern matches a string, the group() method returns the portion of the string that matches the pattern.

The group() method is typically used in conjunction with the search() or match() methods of the re module. Here's a brief explanation of how it works:, string): This function searches for a match of the pattern anywhere in the string. If a match is found, it returns a match object. Once you have a match object, you can use the group() method to retrieve the actual matched substring.

Here's an example to illustrate its usage:

import re

string = "Hello, my name is John Doe"

pattern = r"(\b\w+\b)"

match_object =, string)

if match_object:

    matched_string =

    print(matched_string)  # Output: "Hello"

In this example, the regular expression pattern (\b\w+\b) matches a word in the given string. The group() method retrieves the matched substring "Hello" from the match_object, which is then printed.

You can also specify a group number as an argument to group() if your pattern contains multiple groups. For example, would return the first captured group.

Note that if there are no capturing groups in the pattern, group() with no arguments returns the entire match.
0 votes
by (15.4k points)
The way to access the matched substring in Python's regular expressions is by using the re.findall() function along with non-capturing groups. The findall() function returns a list of all non-overlapping matches of the pattern in the string.

Here's an example:

import re

string = "Hello, my name is John Doe"

pattern = r"\b\w+\b"

matches = re.findall(pattern, string)

if matches:

    print(matches)  # Output: ['Hello', 'my', 'name', 'is', 'John', 'Doe']

In this example, the pattern \b\w+\b matches individual words in the string. Instead of using capturing groups, we only specify the pattern itself. The findall() function returns a list containing all the matched substrings, which in this case are the words in the given string.

Using findall() is advantageous when you want to extract multiple occurrences of a pattern in a string, without explicitly using capturing groups. It simplifies the code and provides a straightforward way to retrieve all the matches.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Nov 12, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Mar 22, 2021 in Java by dante07 (13.1k points)

Browse Categories