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
In the above example, Python’s automatic concatenation of string literals has been used to break up the RE into smaller pieces, but it’s still more difficult to understand than the version using re.VERBOSE. So far we’ve only covered a part of the features of regular expressions. In this section, we’ll cover some new metacharacters, and how to use groups to retrieve portions of the text that was matched.
Top answer
1 of 5
78

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.

🌐
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()
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
Then the if-statement tests the match -- if true the search succeeded and match.group() is the matching text (e.g. 'word:cat'). Otherwise if the match is false (None to be more specific), then the search did not succeed, and there is no matching text. The 'r' at the start of the pattern string designates a python "raw" string which passes through backslashes without change which is very handy for regular expressions (Java needs this feature badly!).
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
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.
🌐
Imperial College London
python.pages.doc.ic.ac.uk › lessons › regex › 07-groups › 02-named.html
Ic
>>> 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'}
🌐
Timothygebhard
timothygebhard.de › posts › named-groups-in-regex-in-python
Named groups for regex in Python · Timothy Gebhard
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:
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python regex groups
Python regex groups - Spark By {Examples}
May 31, 2024 - The syntax for creating groups ... specific information from text. So to create a regex group simply enclose the desired pattern within parentheses ( )....
🌐
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 ...
🌐
TutorialsPoint
tutorialspoint.com › What-is-the-groups-method-in-regular-expressions-in-Python
What is the groups() method in regular expressions in Python?
import re text = "Name: Amit, Age: 30" match = re.search(r'Name: (\w+), Age: (\d+)', text) if match: print(match.groups()) ... In this example, we will learn how to use Python's regular expressions to extract an email address from a string.
🌐
TutorialsPoint
tutorialspoint.com › how-do-we-use-python-regular-expression-named-groups
How do we use Python Regular Expression named groups?
Following is the Python example, which demonstrates how to match a particular pattern in a string using groups - import re input_string = "Tutorials Point began as a single HTML tutorial and has since expanded to offer a wide range of online courses and tutorials. It was established on 2014-06-12." regexp = r"(\d{4})-(\d{2})-(\d{2})" match = re.search(regexp, input_string) if match: print("Found date in the given text:", match.group(0)) print("Year:", match.group(1)) print("Month:", match.group(2)) print("Day:", match.group(3))
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex capturing group
Python Regex Capturing Groups
February 18, 2022 - To get all the named subgroups of a match, you use the groupdict() method of the Match object. For example: import re s = 'news/100' pattern = '(?P<resource>\w+)/(?P<id>\d+)' matches = re.finditer(pattern, s) for match in matches: print(match.groupdict())Code language: Python (python)
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-matchobject-group-function-in-python-regex
re.MatchObject.group() function in Python Regex - GeeksforGeeks
July 15, 2025 - Note the use of '()' the parenthesis ... above example, we have three subgroups in the match pattern. The result we get is a re.MatchObject which is stored in match_object. Note: To know more about regex patterns refer Python regex · Depending on the arguments passed the group method returns ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use Regex Match Objects in Python | note.nkmk.me
May 18, 2023 - re - Match Objects — Regular expression operations — Python 3.11.3 documentation · Contents · Get the matched position: start(), end(), span() Extract the matched string: group() Grouping in regex patterns · Extract each group's string: groups() Get the string and position of any group · Nested groups · Set names for groups · Extract each group's string as a dictionary: groupdict() Match objects in if statements · The sample code in this article uses the following string as an example.
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node82.html
Using Named Groups for Tagging
recpat1 = re.compile(r'(?P<name>\w+) (?P<room>\d+) (?P<phone>x?[0-9-]+)') First, note that using named groups does not override the default behaviour of tagging - the findall function and method will still work in the same way, and you can always refer to the tagged groups by number.
🌐
Reddit
reddit.com › r/learnpython › (regex | regular expression) match.group(1) = 'none' due to using appended patterns
r/learnpython on Reddit: (Regex | Regular expression) match.group(1) = 'None' due to using appended patterns
December 19, 2024 -

Hi there,

I am using regex to get 'pack size' (how a product is packaged) out of product descriptions. So 'Apple 1x300g' and taking out '1x300g'.

The issue is that there are lots of different variants a pack size can be, so I have been using the '|' between the regex expressions. However, when doing this it doesn't allow the match.group(n), where n >1 to work.

Quick example:

description = 'apple 1x300g'

UoM = r'(g|kg|ml) #Meaning Unit's of Measure
all_patterns = (

r'(\d+)\s*x\s*+(\d+)' + UoM + r'|'

r'(\d+)\s*' + UoM +r'\s*x(\d+)'

)

match = re.search(all_patterns, description)

However, match.group(0) will give '1x300g' and match.group(1) will be 'none' as all_patterns is just one big or. I am wanting it to be '1' and '300g'.

Is there a simple fix, other than looping through the patterns?

I am new to regex so appreciate any help.

PL :)

Top answer
1 of 3
3
I'm not sure if reddit messed up your code or something, but there seems some missing escapes in the regex (e.g. d+ rather than \d+). Secondly, there's something strange, 1x300g does not match the pattern you provided, since it requires at least one whitespace character after the 'x'. The groups when there are alternatives are not independent. For example, in the regex a(\d)|b(\d) there are two groups, group(1) would be the digit after an 'a', and group(2) would be the digit after a 'b'. group(0) is always the entire match. e.g. >>> re.search("a(\d)|b(\d)", "a1").groups() ('1', None) >>> re.search("a(\d)|b(\d)", "b1").groups() (None, '1') The built-in 're' module does not support overlapping groups, but you can name them: >>> re.search("a(?P\d)|b(?P\d)", "a1").groupdict() {'adigit': '1', 'bdigit': None} >>> re.search("a(?P\d)|b(?P\d)", "b1").groupdict() {'adigit': None, 'bdigit': '1'} So you can first figure out whether you are in the 'a' or 'b' case, then access the correct group. The third-party 'regex' package does support overlapping groups, which means you can make code a bit more robust: >>> import regex >>> regex.search("a(?P\d)|b(?P\d)", "a1").groupdict() {'digit': '1'} >>> regex.search("a(?P\d)|b(?P\d)", "b1").groupdict() {'digit': '1'}
2 of 3
2
Is there a simple fix, other than looping through the patterns? I recommend doing this instead. It keeps each regex simple. Make a function for each alternative, and attempt to apply each of them. If you run into performance problems, break up the text line-by-line so that each search is relatively small.
🌐
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.")