You need the first captured group:

a.group(1)
b.group(1)
...

without any captured group specification as argument to group(), it will show the full match, like what you're getting now.

Here's an example:

In [8]: string_one = 'file_record_transcript.pdf'

In [9]: re.search(r'^(file.*)\.pdf$', string_one).group()
Out[9]: 'file_record_transcript.pdf'

In [10]: re.search(r'^(file.*)\.pdf$', string_one).group(1)
Out[10]: 'file_record_transcript'
Answer from heemayl on Stack Overflow
🌐
PYnative
pynative.com › home › python › regex › python regex capturing groups
Python Regex Capturing Groups – PYnative
April 12, 2021 - Python regex capturing groups match several distinct patterns inside the same target string using group() and groups()
🌐
Python documentation
docs.python.org › 3 › howto › regex.html
Regular expression HOWTO — Python 3.14.7 documentation
Let’s take an example: \w matches any alphanumeric character. If the regex pattern is expressed in bytes, this is equivalent to the class [a-zA-Z0-9_]. If the regex pattern is a string, \w will match all the characters marked as letters in the Unicode database provided by the unicodedata module.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-regex-replace-captured-groups
Python Regex: Replace Captured Groups - GeeksforGeeks
July 23, 2025 - A group in regex is a part of a pattern that is enclosed in parentheses. Groups allow us to segment a pattern into sub-patterns, making it easier to apply specific operations on each part.
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-matchobject-group-function-in-python-regex
re.MatchObject.group() function in Python Regex - GeeksforGeeks
July 15, 2025 - re.MatchObject.group() method returns the complete matched subgroup by default or a tuple of matched subgroups depending on the number of arguments
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex groups
Python regex groups - Spark By {Examples}
May 31, 2024 - Python regex groups offer powerful capabilities for extracting specific information from text and applying advanced pattern-matching techniques. With
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
The "group" feature of a regular expression allows you to pick out parts of the matching text. Suppose for the emails problem that we want to extract the username and host separately.
🌐
Wellsr
wellsr.com › python › using-python-regex-groups-to-capture-substrings
Using Python Regex Groups to Capture Substrings - wellsr.com
June 28, 2019 - In this section we will describe how to define a group, and how to retrieve its substring with the match.group() method, referring to it either by index or name. We can create a Python regex group by enclosing part of a regex between parenthesis; ...
Find elsewhere
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex capturing group
Python Regex Capturing Groups
February 18, 2022 - For example, to create a capturing group that captures the id from the path, you use the following pattern: ... In this pattern, we place the rule \d+ inside the parentheses (). If you run the program with the new pattern, you’ll see that it displays one match: import re s = 'news/100' pattern = '\w+/(\d+)' matches = re.finditer(pattern, s) for match in matches: print(match)Code language: Python (python)
🌐
Imperial College London
python.pages.doc.ic.ac.uk › lessons › regex › 07-groups › 02-named.html
Advanced Lesson 1: Regular Expressions > Named groups | Python Programming | Department of Computing | Imperial College London
>>> pattern = "Name: (?P<name>[A-Za-z ]+); Phone: (?P<phone>\d+)" >>> string = "Name: Josiah Wang; Phone: 012345678" >>> match = re.match(pattern, string) >>> print(match) <re.Match object; span=(0, 35), match='Name: Josiah Wang; Phone: 012345678'> >>> match.group("name") 'Josiah Wang' >>> match.group("phone") '012345678' >>> match.group(1) 'Josiah Wang' >>> match.group(2) '012345678' >>> match.groupdict() {'name': 'Josiah Wang', 'phone': '012345678'}
🌐
Kodeclik
kodeclik.com › python-regex-group
Python Regex groups
October 16, 2024 - A Python regex group is a part of a regex pattern that we wish to reference or access later and is enclosed in parentheses. Use Python regex groups to extract specific parts of your string and use them in your program.
🌐
Finxter
blog.finxter.com › home › learn python blog › python re groups
Python Re Groups – Be on the Right Side of Change
May 5, 2023 - ... Like you use parentheses to ... regex that does this is 'a(b|c)'. The whole content enclosed in the opening and closing parentheses is called matching group (or capture group)....
🌐
LearnByExample
learnbyexample.github.io › py_regular_expressions › groupings-and-backreferences.html
Groupings and backreferences - Understanding Python re(gex)?
It may be obvious, but it should ... capture group. For example, if (\d[a-f]) matches 3b, then backreferencing will give 3b and not any other valid match of RE like 8f, 0a etc. This is akin to how variables behave in programming, only the result of an expression stays after variable assignment, not the expression itself. The regex module supports ...
🌐
Timothygebhard
timothygebhard.de › posts › named-groups-in-regex-in-python
Named groups for regex in Python · Timothy Gebhard
July 23, 2022 - I figured that maybe it is about time I just write down the correct syntax myself once, so that either my brain will now remember it, or that I at least know where to look it so. So without further ado, here’s the example code for named group using Python’s re module:
🌐
Safjan
safjan.com › home › note › python regex named groups
Python Regex Named Groups - Krystian Safjan's Blog
July 11, 2023 - import re pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})' text = 'Today is 2023-04-19' match = re.search(pattern, text) if match: print(match.groupdict())
Top answer
1 of 5
79

You could create a little class that returns the boolean result of calling match, and retains the matched groups for subsequent retrieval:

import re

class REMatcher(object):
    def __init__(self, matchstring):
        self.matchstring = matchstring

    def match(self,regexp):
        self.rematch = re.match(regexp, self.matchstring)
        return bool(self.rematch)

    def group(self,i):
        return self.rematch.group(i)


for statement in ("I love Mary", 
                  "Ich liebe Margot", 
                  "Je t'aime Marie", 
                  "Te amo Maria"):

    m = REMatcher(statement)

    if m.match(r"I love (\w+)"): 
        print "He loves",m.group(1) 

    elif m.match(r"Ich liebe (\w+)"):
        print "Er liebt",m.group(1) 

    elif m.match(r"Je t'aime (\w+)"):
        print "Il aime",m.group(1) 

    else: 
        print "???"

Update for Python 3 print as a function, and Python 3.8 assignment expressions - no need for a REMatcher class now:

import re

for statement in ("I love Mary",
                  "Ich liebe Margot",
                  "Je t'aime Marie",
                  "Te amo Maria"):

    if m := re.match(r"I love (\w+)", statement):
        print("He loves", m.group(1))

    elif m := re.match(r"Ich liebe (\w+)", statement):
        print("Er liebt", m.group(1))

    elif m := re.match(r"Je t'aime (\w+)", statement):
        print("Il aime", m.group(1))

    else:
        print()
2 of 5
33

Less efficient, but simpler-looking:

m0 = re.match("I love (\w+)", statement)
m1 = re.match("Ich liebe (\w+)", statement)
m2 = re.match("Je t'aime (\w+)", statement)
if m0:
  print("He loves", m0.group(1))
elif m1:
  print("Er liebt", m1.group(1))
elif m2:
  print("Il aime", m2.group(1))

The problem with the Perl stuff is the implicit updating of some hidden variable. That's simply hard to achieve in Python because you need to have an assignment statement to actually update any variables.

The version with less repetition (and better efficiency) is this:

pats = [
    ("I love (\w+)", "He Loves {0}" ),
    ("Ich liebe (\w+)", "Er Liebe {0}" ),
    ("Je t'aime (\w+)", "Il aime {0}")
 ]
for p1, p3 in pats:
    m = re.match(p1, statement)
    if m:
        print(p3.format(m.group(1)))
        break

A minor variation that some Perl folk prefer:

pats = {
    "I love (\w+)" : "He Loves {0}",
    "Ich liebe (\w+)" : "Er Liebe {0}",
    "Je t'aime (\w+)" : "Il aime {0}",
}
for p1 in pats:
    m = re.match(p1, statement)
    if m:
        print(pats[p1].format(m.group(1)))
        break

This is hardly worth mentioning except it does come up sometimes from Perl programmers.

🌐
Imperial College London
python.pages.doc.ic.ac.uk › 2021 › lessons › regex › 07-groups › 01-group.html
Advanced Lesson 1: Regular Expressions > Capturing groups | Python Programming (70053 Autumn Term 2021/2022) | Department of Computing | Imperial College London
>>> pattern = "Name: ([A-Za-z ]+); Phone: (\d+); Position: (.+)" >>> string = "Name: Josiah Wang; Phone: 012345678; Position: Senior Teaching Fellow" >>> match = re.match(pattern, string) >>> print(match) <re.Match object; span=(0, 69), match='Name: Josiah Wang; Phone: 012345678; Position: Se> >>> match.groups() ('Josiah Wang', '012345678', 'Senior Teaching Fellow') >>> match.group() 'Name: Josiah Wang; Phone: 012345678; Position: Senior Teaching Fellow' >>> match.group(1) 'Josiah Wang' >>> match.group(2) '012345678' >>> match.group(3) 'Senior Teaching Fellow' >>> match.group(1,3) ('Josiah Wang', 'Senior Teaching Fellow') >>> match.start(), match.end() (0, 69) >>> match.start(1), match.end(1) (6, 17) >>> match.start(2), match.end(2) (26, 35) >>> match.start(3), match.end(3) (47, 69) >>> match.span(2) # another way of getting a tuple (start, end) (26, 35) >>> match.span(3) (47, 69)
🌐
Real Python
realpython.com › lessons › regex-grouping
Regex Grouping (Video) – Real Python
In the previous lesson, I showed you how to multiply your regular expression using quantifiers. In this lesson, I’ll show you how to group parts of your regular expression together in subsets. First, a little review of quantifiers. The asterisk (*…
Published: November 17, 2020
🌐
py4u
py4u.org › blog › python-regexp-groups-how-do-i-get-all-groups
Python Regex Groups: How to Extract All Matching Groups from a String Using Regular Expressions
Among their most useful features are **regex groups**—subpatterns enclosed in parentheses that allow you to extract specific parts of a match, rather than just the entire matched string.
🌐
LabEx
labex.io › tutorials › python-how-to-use-regex-capture-groups-in-python-420906
How to use regex capture groups in Python | LabEx
Capture groups are a powerful feature in regular expressions that allow you to extract and group specific parts of a matched pattern. In Python, they are defined using parentheses () within a regex pattern.