PythonRegEx Match Object
Match Object
A Match Object is an object containing information about the search and the result.
Example
Do a search that will return a Match Object:
txt = "The rain in Spain"
x = re.search("ai", txt)
print(x) #this will print an object
Note: If there is no match, the valueNone
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
Example
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":
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.span())
Example
Print the string passed into the function:
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.string)
Example
Print the part of the string where there was a match.
The regular expression looks for any words that starts with an upper case "S":
txt = "The rain in Spain"
x = re.search(r"\bS\w+", txt)
print(x.group())
Note: If there is no match, the valueNone
will be returned, instead of the Match Object.
Related Pages
Python RegEx TutorialRegExRegEx FunctionsMetacharacters in RegExRegEx Special SequencesRegEx Sets