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
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ regex.html
Regular Expression HOWTO โ€” Python 3.14.3 documentation
The groups() method returns a tuple containing the strings for all the subgroups, from 1 up to however many there are. ... Backreferences in a pattern allow you to specify that the contents of an earlier capturing group must also be found at ...
Discussions

Regex: Capture multiple matches of a single group
Maybe I'm misunderstanding, but that should work in Python too. import re rex = re.compile(r'(.*?)<\/td>') text = """apples bananas carrots dates""" print(rex.findall(text)) #['apples', 'bananas', 'carrots', 'dates'] More on reddit.com
๐ŸŒ r/learnpython
5
3
November 8, 2015
Regex capture / non-capture groups best practice
I've been playing with regex, reading forums and trying out various code on regex101. In Pi-hole I currently have the following blacklist entry (just one example of many) ^(meetings|hangouts|suggestqueries).*google(apis)?\.com$ I hadn't appreciated until now that this creates two capture groups. More on discourse.pi-hole.net
๐ŸŒ discourse.pi-hole.net
1
0
February 18, 2023
regex capture groups
Wouldn't something like for line in lines: match = start_rx.search(line): if match: Work? Also in python 3.8+ there's a new 'walrus operator' which combines lines 2 and 3 of my example into one: for line in lines: if match := start_rx.search(line): Personally I'm not 100% on board with this operator yet but it exists. More on reddit.com
๐ŸŒ r/learnpython
7
2
December 22, 2018
Accessing a "symbolic group name" in Python regex
You use them with Match objects. findall doesn't return those, it returns strings only. For what you want, you would need to use finditer which does return Match objects. However, this is a generator so you couldn't index it, but you can iterate it: for match in found: print(f'Data Content:\t{match["value"]}') More on reddit.com
๐ŸŒ r/learnpython
6
1
November 28, 2022
๐ŸŒ
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:
๐ŸŒ
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()
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-regex-replace-captured-groups
Python Regex: Replace Captured Groups - GeeksforGeeks
July 23, 2025 - Regex is supported in almost all major programming languages, and in Python, the `re` module provides an extensive set of functionalities for regex operations. This article dives into one of the crucial aspects of regex in Python: Regex Groups, and demonstrates how to use `re.sub()` for group replacement with practical examples.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ regex: capture multiple matches of a single group
r/learnpython on Reddit: Regex: Capture multiple matches of a single group
November 8, 2015 -

I'm frustrated trying to do something that I think should be easy. Any help is greatly appreciated!

I want to find all the instances of a string that match my regex and capture part of the match in a group. In perl it's an easy one-liner:

#stores the contents of every <td> element in the array @results
my @results = $inputString =~ m/<td>(.*?)<\/td>/g; 

My simplified example could be accomplished using various html-parsing libraries, but my actual parsing job can't. So far the only Python solution I can find is to use re.findall and then individually parse out my group from the whole match. Is there a better way to do this in Python?

๐ŸŒ
Rexegg
rexegg.com โ€บ regex-capture.php
Regex Capture Groups and Back-References
Capture groups and back-references are some of the more fun features of regular expressions. You place a sub-expression in parentheses, you access the capture with \1 or $1โ€ฆ What could be easier? For instance, the regex \b(\w+)\b\s+\1\b matches repeated words, such as regex regex, because ...
Find elsewhere
๐ŸŒ
Safjan
safjan.com โ€บ home โ€บ note โ€บ python regex named groups
Python Regex Named Groups
July 11, 2023 - In Python regex, match.groupdict() is a method that returns a dictionary containing all the named groups of a regular expression match. When you use named capturing groups in a regular expression using the (?P<name>...) syntax, you can access ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
4 days ago - The result depends on the number of capturing groups in the pattern. If there are no groups, return a list of strings matching the whole pattern. If there is exactly one group, return a list of strings matching that group. If multiple groups are present, return a list of tuples of strings matching the groups.
๐ŸŒ
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.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python regex โ€บ python regex capturing group
Python Regex Capturing Groups
February 18, 2022 - In this pattern, we have two capturing groups one for \w+ and the other for \d+ . The following program shows the entire match and all the subgroups: import re s = 'news/100' pattern = '(\w+)/(\d+)' matches = re.finditer(pattern, s) for match in matches: for index in range(0, match.lastindex + 1): print(match.group(index))Code language: Python (python)
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python regular expressions
Python Regular Expressions | Python Education | Google for Developers
The re.findall(pat, str) function finds all matches of a pattern in a string and returns them as a list of strings or tuples, depending on whether the pattern contains capturing groups. Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and shows how regular expressions work in Python.
๐ŸŒ
Regular-Expressions.info
regular-expressions.info โ€บ named.html
Regex Tutorial: Named Capturing Groups - Backreference Names
The .NET syntax with angle brackets is supported since the 2023-09-01 release. Since RE2 uses a text-directed engine, it does not support backreferences at all. You can use RE2::NamedCapturingGroups() to retrieve matches of named capturing groups in your code after the regex has found a match.
๐ŸŒ
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 ... the 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 ...
๐ŸŒ
Medium
medium.com โ€บ @MynaviTechTusVietnam โ€บ regex-for-dummies-part-4-capturing-groups-and-backreferences-50c338a3b6f6
Regex For Dummies. Part 4: Capturing Groups and Backreferences | by Mynavi TechTus Vietnam | Medium
October 17, 2023 - In a regex pattern, you can reference a capturing group by using a backslash followed by the group number. Group numbers are assigned based on the order of opening parentheses in the regex pattern, starting from 1.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python regex groups
Python regex groups - Spark By {Examples}
May 31, 2024 - If a match is found, we access the captured groups using the match.group() method. Group 1 corresponds to the first set of parentheses, Group 2 corresponds to the second set of parentheses, and Group 3 corresponds to the third set of parentheses in the pattern. ... Named groups in Python regex allow us to assign names to specific capturing groups within a pattern.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_regex.asp
Python RegEx
.span() returns a tuple containing the start-, and end positions of the match. .string returns the string passed into the function .group() returns the part of the string where there was a match
๐ŸŒ
Imperial College London
python.pages.doc.ic.ac.uk โ€บ lessons โ€บ regex โ€บ 07-groups โ€บ 02-named.html
Advanced Lesson 1: Regular Expressions > Named groups
>>> 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'}
๐ŸŒ
RegexOne
regexone.com โ€บ lesson โ€บ capturing_groups
RegexOne - Learn Regular Expressions - Lesson 11: Match groups
You could then use a pattern such as ^(IMG\d+\.png)$ to capture and extract the full filename, but if you only wanted to capture the filename without the extension, you could use the pattern ^(IMG\d+)\.png$ which only captures the part before the period. Go ahead and try to use this to write a regular expression that matches only the filenames (not including extension) of the PDF files below. ... Solve the above task to continue on to the next problem, or read the Solution. Next โ€“ Lesson 12: Nested groups Previous โ€“ Lesson 10: Starting and ending ยท Find RegexOne useful?