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
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-use-regex-capture-groups-in-python-420906
How to use regex capture groups in Python | LabEx
In Python, they are defined using parentheses () within a regex pattern. Let's start by creating a Python script to demonstrate basic capture group usage. Open the integrated terminal in the WebIDE and navigate to the project directory if you are not already there. ... Create a new file named basic_capture.py using the touch command. ... import re text = "Contact email: john.doe@example.com" pattern = r"(\w+)\.(\w+)@(\w+)\.(\w+)" match = re.search(pattern, text) if match: username = match.group(1) lastname = match.group(2) domain = match.group(3) tld = match.group(4) print(f"Username: {username}") print(f"Lastname: {lastname}") print(f"Domain: {domain}") print(f"TLD: {tld}") else: print("No match found.")
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ regex.html
Regular Expression HOWTO โ€” Python 3.14.3 documentation
Backreferences in a pattern allow ... location in the string. For example, \1 will succeed if the exact contents of group 1 can be found at the current position, and fails otherwise....
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ regex โ€บ python regex capturing groups
Python Regex Capturing Groups โ€“ PYnative
April 12, 2021 - # Extract first group print(result.group(1)) # Extract second group print(result.group(2)) # Target string print(result.group(0))Code language: Python (python) So this is the simple way to access each of the groups as long as the patterns were matched. In earlier examples, we used the search method. It will return only the first match for each group. But what if a string contains the multiple occurrences of a regex group and you want to extract all matches. In this section, we will learn how to capture all matches to a regex 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 ... 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.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python regex replace with capture group
Python regex replace with capture group - Spark By {Examples}
May 31, 2024 - Hereโ€™s an example to demonstrate using capture groups in re.sub(): import re # the original text text = "Hello, John Doe!" # define the regex pattern with a capture group for the name pattern = r"Hello, (\w+) (\w+)!" # specify the replacement ...
๐ŸŒ
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 - This is especially useful when you want to apply quantifiers or modifiers to multiple characters or subpatterns. For example, (abc)+ matches one or more repetitions of "abc" as a whole.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python regex โ€บ python regex capturing group
Python Regex Capturing Groups
February 18, 2022 - 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) ... In the output, the news/100 is the entire match while news and 100 are the subgroups. By default, you can access a subgroup in a match using an index, for example, match.group(1). Sometimes, accessing a subgroup by a meaningful name is more convenient. You use the named capturing group to assign a name to a group.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-regex-replace-captured-groups
Python Regex: Replace Captured Groups - GeeksforGeeks
July 23, 2025 - Regex is supported in almost all ... 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....
๐ŸŒ
Regular-Expressions.info
regular-expressions.info โ€บ named.html
Regex Tutorial: Named Capturing Groups - Backreference Names
RE2 supports named capturing groups using the Python syntax. 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.
๐ŸŒ
Wellsr
wellsr.com โ€บ python โ€บ using-python-regex-groups-to-capture-substrings
Using Python Regex Groups to Capture Substrings - wellsr.com
June 28, 2019 - In other words, we could rewrite the previous example as \s*(?P<g>(a|b)?c)\s*, so that the group ยท g captures the whole string, while second group catches only the optional prefix. But Python offers a better option: with the (?:regex) syntax we notify Python not to capture the substring for
๐ŸŒ
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
Enclose the desired pattern in parentheses () to create a capturing group. Use re.search() to find matches, and access captured groups with the .group() method or by indexing the result. For example: match = re.search(r'(\d+)', 'abc123') captures the digits, and match.group(1) returns '123'.
๐ŸŒ
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'}
๐ŸŒ
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?

๐ŸŒ
RegexOne
regexone.com โ€บ references โ€บ python
RegexOne - Learn Regular Expressions - Python
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 an empty list if no matches are found. If you need additional context for each match, you can use re.finditer() which instead returns an iterator of re.MatchObjects to walk through. Both methods take the same parameters. ... import re # Lets use a regular expression to match a few date strings. regex = r"[a-zA-Z]+ \d+" matches = re.findall(regex, "June 24, August 9, Dec 12") for match in matches: # This will print: # June 24 #
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations โ€” Python 3.14.3 ...
The value of endpos which was passed to the search() or match() method of a regex object. This is the index into the string beyond which the RE engine will not go. ... The integer index of the last matched capturing group, or None if no group was matched at all. For example, the expressions (a)b, ((a)(b)), and ((ab)) will have lastindex == 1 if applied to the string 'ab', while the expression (a)(b) will have lastindex == 2, if applied to the same string.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python re groups
Python Re Groups - Be on the Right Side of Change
May 5, 2023 - What if you just need to keep your regex pattern in orderโ€”but you donโ€™t want to capture the contents of a matching group? The simple solution is the non-capturing group operation (?: ... ). You can use it just like the capturing group operation ( ... ). Hereโ€™s an example: >>>import re >>> re.search('(?:python...
๐ŸŒ
Linux find Examples
queirozf.com โ€บ entries โ€บ python-regular-expressions-examples-reference
Python Regular Expressions: Examples & Reference
April 21, 2022 - import re # this pattern matches things like "foo-bar" or "bar-baz" pattern = "^(\w{3})-(\w{3})$" string1 = "abc-def" # returns Null if no match matches = re.match("^(\w{3})-(\w{3})$",string1) if matches: # match indices start at 1 first_group_match = matches.group(1) # abc second_group_match = matches.group(2) # def print(first_group_match+" AND "+second_group_match) # prints: "abc AND def" Only the first occurrence of the capture can be extracted.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python regex groups
Python regex groups - Spark By {Examples}
May 31, 2024 - The matches are returned as a list of tuples, with each tuple containing the captured first name and last name. The code then iterates over each match and unpacks the first name and last name from each tuple. ... #Output First name: Dennis Last name: Ritchie First name: Brian Last name: Yu First name: Bill Last name: Gates First name: Stevie Last name: Jobs ยท Nested groups in Python regular expressions allow you to define groups within groups, creating a hierarchical structure for pattern matching and capturing.