A Match Object is an object containing information about the search and the result.
ExampleDo a search that will return a Match Object:
import re
txt = "The rain in Spain"
x = re.search("ai", txt)
print(x) #this will print an object
Try it Yourself »Note: If there is no match, the value None
will be returned, instead of the Match Object.
The Match object has properties and methods used to retrieve information about the search, and the result:
.span()
returns a tuple containing the start-, and end positions of the match..string
returns the string passed into the function.group()
returns the part of the string where there was a match
Print the position (start- and end-position) of the first match occurrence.
The regular expression looks for any words that starts with an upper case "S":
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(
x.span())
Try it Yourself » ExamplePrint the string passed into the function:
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(
x.string)
Try it Yourself » ExamplePrint the part of the string where there was a match.
The regular expression looks for any words that starts with an upper case "S":
import re
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(
x.group())
Try it Yourself »Note: If there is no match, the value None
will be returned, instead of the Match Object.
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4