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 - To capture all matches to a regex group we need to use the finditer() method. The finditer() method finds all matches and returns an iterator yielding match objects matching the regex pattern. Next, we can iterate each Match object and extract its value. Note: Donโ€™t use the findall() method ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-use-regex-capture-groups-in-python-420906
How to use regex capture groups in Python | LabEx
Run the script using the python command. ... This output shows that the script successfully extracted the product name and price using named capture groups. You can access the captured data using the group name as a key in the group() method. Named capture groups make your regex patterns and the subsequent code more understandable, especially for complex patterns with many capture groups.
๐ŸŒ
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 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-regex-replace-captured-groups
Python Regex: Replace Captured Groups - GeeksforGeeks
July 23, 2025 - You can use backreferences to include captured groups in the replacement. `string`: The original string where the replacement is performed. `count`: The maximum number of replacements. Default is `0`, which means replace all occurrences. `flags`: Optional flags to modify the regex behavior. Python ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations โ€” Python 3.14.3 ...
Return all non-overlapping matches of pattern in string, as a list of strings or tuples. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result. The result depends on the number of capturing groups in the 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)
๐ŸŒ
Regular-Expressions.info
regular-expressions.info โ€บ named.html
Regex Tutorial: Named Capturing Groups - Backreference Names
If you use the submatch argument to retrieve group matches in your code then the resulting array will have a separate entry for each capturing group, even for groups that have the same name as another group. Python, Java, and ICU do not allow multiple groups to use the same name. Doing so will give a regex compilation error.
Find elsewhere
๐ŸŒ
Wellsr
wellsr.com โ€บ python โ€บ using-python-regex-groups-to-capture-substrings
Using Python Regex Groups to Capture Substrings - wellsr.com
June 28, 2019 - This tutorial describes how to use Python Regex Groups to capture a substring or submatch, and it describes how to refer to a group's substring inside a regex.
๐ŸŒ
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 - Named capturing groups allow you to assign names to your capturing groups, making it easier to reference and work with specific matched portions of text. Instead of referring to capturing groups by their numerical indices, you can use descriptive names, which enhances the readability and maintainability of your regex patterns.
๐ŸŒ
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 ...
๐ŸŒ
InfoWorld
infoworld.com โ€บ home โ€บ software development โ€บ programming languages โ€บ python
Python regex: How to use Python regular expressions | InfoWorld
February 24, 2021 - Capture groups let you use parentheses to indicate different parts of a match: match.group() or match.group(0) returns the entire match, match.group(1) returns the first capture group, a combination of arguments (match.group(1,2)) produces a ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-regex-capturing-groups-a-helpful-guide-video
Python Regex Capturing Groups โ€“ A Helpful Guide (+Video) โ€“ Be on the Right Side of Change
By using capturing groups, you ... to isolate and extract relevant data from a given text. To define a capturing group, I simply place the desired regex rule within parentheses, like this: (rule)....
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python re groups
Python Re Groups - Be on the Right Side of Change
May 5, 2023 - You can then retrieve the captured groups with the \number syntax within the regex pattern itself and with the m.group(i) syntax in the Python code at a later stage.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ regex capture groups
r/learnpython on Reddit: regex capture groups
December 22, 2018 -

Hi

I have used Perl extensively in the past but now I move to python and I am probably doing regex-stuff wrong...

I have a list of lines that I want to match against a regex and capture some variables if the regex matches (not all lines match).

I am doing this:

start_rx = re.compile("silence_start: ([\d\.]+)")

for line in lines:

  if start_rx.search(line):

     start_time = start_rx.search(line).group(1)

     print(start_time)

And that works, but what I don't like is that I call search() twice - once to see if the line matches at all (otherwise I get an exception on the lines that don't match) and once to retrieve the capture-group.

Surely there is a better way to do this?

๐ŸŒ
RegexOne
regexone.com โ€บ references โ€บ python
RegexOne - Learn Regular Expressions - Python
:(") Unlike the re.search() method above, we can use re.findall() to perform a global search over the whole input string. If there are capture groups in the pattern, then it will return a list of all the captured data, but otherwise, it will just return a list of the matches themselves, or ...
๐ŸŒ
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'}
๐ŸŒ
Rexegg
rexegg.com โ€บ regex-capture.php
Regex Capture Groups and Back-References
To see how this works, see the CaptureCollection section of the C# page. Perl, PHP, R, Python: Group Numbering with Subroutines and Recursion Some enginesโ€”such as Perl, PCRE (PHP, R, Delphiโ€ฆ) and Matthew Barnett's regex module for Pythonโ€”allow you to repeat a part of a pattern (a subroutine) ...