Assuming you meant to find the phrase "The rain falls in Spain" within a larger string:

import re

t = "Hello G'day. The rain falls in Spain. Testing 123."
x = re.search("The.*Spain", t)

if x:
  print("There's a match!")
  print(f'The match is: {x.group(0)!r}')
  print('The span is:', x.span(0))
else:
  print("There's no match")

print(x)

Output:

There's a match!
The match is: 'The rain falls in Spain'
The span is: (13, 36)
<re.Match object; span=(13, 36), match='The rain falls in Spain'>

What did I change:

  • I removed ^$ from the regex, because we don't want to check for the start and end of a string explicitly. The desired match can also be somewhere within the string as well.
  • Access a captured group number n using the syntax x.group(n). In the above example, you can also use x.group() as a shortcut to get the first captured group (i.e. the first match)
  • Access the start/end indices within the input string, of a captured group n (i.e. a match), using the syntax x.span(n). Similarly as above, you can use x.span() as a shortcut to get the span of the first match.
Answer from Wizard.Ritvik on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_regex_match.asp
Python RegEx Match Object
Python Examples Python Compiler ... Python Certificate Python Training ... A Match Object is an object containing information about the search and the result....
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
The regex matching flags. This is a combination of the flags given to compile(), any (?...) inline flags in the pattern, and implicit flags such as UNICODE if the pattern is a Unicode string. ... The number of capturing groups in the pattern. ... A dictionary mapping any symbolic group names defined by (?P<id>) to group numbers. The dictionary is empty if no symbolic groups were used in the pattern. ... The pattern string from which the pattern object was compiled.
🌐
Reddit
reddit.com › r/learnpython › how to get the actual match from a re.match object?
r/learnpython on Reddit: How to get the actual match from a re.match object?
October 18, 2023 -

Assuming that match is a regular expression match object, the following works:

match.string[match.start():match.end()]

...but this seems like an overly complicated way to do something that should have its own built-in method. Is there a better way to refactor the expression above?

🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use Regex Match Objects in Python | note.nkmk.me
May 18, 2023 - In Python's re module, match() and search() return match objects when a string matches a regular expression pattern. You can extract the matched string and its position using methods provided by the m ...
🌐
Python
docs.python.org › 2.0 › lib › match-objects.html
4.2.5 Match Objects
Return the indices of the start and end of the substring matched by group; group defaults to zero (meaning the whole matched substring). Return -1 if group exists but did not contribute to the match. For a match object m, and a group g that did contribute to the match, the substring matched ...
🌐
Python
peps.python.org › pep-0636
PEP 636 – Structural Pattern Matching: Tutorial | peps.python.org
To prevent this problem you can ... would raise. ... The match statement evaluates the “subject” (the value after the match keyword), and checks it against the pattern (the code next to case)....
🌐
Code.mu
code.mu › en › python › manual › regular › match-object
Match-object - information about matches of a regular expression in a string in Python
Match-object of the re module object is an object that contains information about regular expression matches in a string in Python.
Find elsewhere
🌐
Xah Lee
xahlee.info › python › python_regex_match_object.html
Python: Regex Match Object
The string passed to match() or search(). Example: # -*- coding: utf-8 -*- # python 2 import re mm = re.compile(r'some.+').search('some text') print mm.string # prints 'some text' The regular expression object whose match() or search() method produced this MatchObject instance.
🌐
GeeksforGeeks
geeksforgeeks.org › python › re-match-in-python
re.match() in Python - GeeksforGeeks
July 23, 2025 - The result of re.match is a match object if a match is found, or None if no match is found. If a match occurs, we can get more information about it. ... import re s = "Python is great" match = re.match(r"Python", s) if match: print(f"Match found: ...
🌐
PYnative
pynative.com › home › python › regex › python regex match: a comprehensive guide for pattern matching
Python Regex Match: A Comprehensive guide for pattern matching
April 2, 2021 - The search() checks for a match anywhere in the string. ... The match method returns a corresponding match object instance if zero or more characters at the beginning of the string match the regular expression pattern.
🌐
Plain English Westminster
benhoyt.com › writings › python-pattern-matching
Structural pattern matching in Python 3.10
The cases themselves are a bit nicer, especially how obj is bound automatically instead of using objects[0]. However, one thing that’s not great is how the “success case” ends up sandwiched in the middle, so the normal code path gets a bit lost. You could hack it to the end with the following (but then it’s definitely not as clear as the original): match objects: case []: raise ValueError('object {!r} not found'.format(sha1_prefix)) case [_, _, *_]: raise ValueError('multiple objects ({}) with prefix {!r}' .format(len(objects), sha1_prefix)) case [obj]: return os.path.join(obj_dir, obj)
🌐
Finxter
blog.finxter.com › home › learn python blog › python regex match
Python Regex Match - Be on the Right Side of Change
November 20, 2020 - The match object is a simple wrapper for this information. Some regex methods of the re module in Python—such as re.match()—automatically create a match object upon the first pattern match.
🌐
Codecademy
codecademy.com › docs › python › regular expressions › re.match()
Python | Regular Expressions | re.match() | Codecademy
September 5, 2023 - The following example returns a match object (<re.Match object; span=(0, 12), match='123-456-7890'>) and not None since the phone number (123-456-7890) matches the test pattern: ... Learn the basics of regular expressions and how to pull and clean data from the web with Python.
🌐
Built In
builtin.com › articles › python-re-match
Python re.match() and re.sub() Explained | Built In
re.sub(): Python’s re.sub() function substitutes occurrences of a pattern in a string. The re.match() function checks for a match only at the beginning of the string. If the match is found at the start of the string, it returns a match object.
🌐
Python for Network Engineers
pyneng.readthedocs.io › en › latest › book › 15_module_re › match_object.html
Match object - Python for network engineers
In [1]: log = 'Jun 3 14:39:05.941: %SW_MATM-4-MACFLAP_NOTIF: Host f03a.b216.7ad7 in vlan 10 is flapping between port Gi0/5 and port Gi0/15' In [2]: match = re.search(r'Host (\S+) in vlan (\d+) .* port (\S+) and port (\S+)', log) In [3]: match Out[3]: <_sre.SRE_Match object; span=(47, 124), match='Host f03a.b216.7ad7 in vlan 10 is flapping betwee>'
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
The search() function searches the string for a match, and returns a Match object if there is a match.
🌐
Interactive Chaos
interactivechaos.com › en › python › function › rematch
re.match - Python
Python scenarios · Full name · re.match · Library · re · Syntax · re.match(pattern, string, flags=0) Description · The re.match function checks if the regular expression pattern is satisfied at the beginning of the text string, returning the corresponding match object if it is positive.
🌐
LearnByExample
learnbyexample.github.io › py_regular_expressions › working-with-matched-portions.html
Working with matched portions - Understanding Python re(gex)?
The re.search() and re.fullmatch() functions return a re.Match object from which various details can be extracted like the matched portion of string, location of the matched portion, etc. Note that you'll get the details only for the first match. Working with multiple matches will be covered later in this chapter.