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
nusing the syntaxx.group(n). In the above example, you can also usex.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 syntaxx.span(n). Similarly as above, you can usex.span()as a shortcut to get the span of the first match.
Videos
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?
You should use re.MatchObject.group(0). Like
imtag = re.match(r'<img.*?>', line).group(0)
Edit:
You also might be better off doing something like
imgtag = re.match(r'<img.*?>',line)
if imtag:
print("yo it's a {}".format(imgtag.group(0)))
to eliminate all the Nones.
Note that re.match(pattern, string, flags=0) only returns matches at the beginning of the string. If you want to locate a match anywhere in the string, use re.search(pattern, string, flags=0) instead (https://docs.python.org/3/library/re.html). This will scan the string and return the first match object. Then you can extract the matching string with match_object.group(0) as the folks suggested.