Python RegEx - Match Object
A Match Object is an object containing information about the search and the result. If there is no match, the value None will be returned, instead of the Match Object.
Match Object instances support the following methods and attributes:
- start(): Returns the index number for start position of the first match.
- end(): Returns the index number for end position of the first match.
- span(): Returns a tuple containing start and end position of the first match.
- string(): Returns a string passed to the match() or search() function.
- group(): Returns the part of the string where there was a match.
- endpos(): Returns the index number which was passed to the match() or search()function. regex engine will not go beyond endpos.
The start(), end() & span() Methods
The example below describes the usage of start(), end() and span() methods while working with search() function.
import re MyString = "31 January, 28 February, 31 March" #find first match of comma (,) x = re.search(",", MyString) #print start position of first match print("First comma start point:", x.start()) #print end position of first match print("First comma end point:", x.end()) #print start position of first match print("First comma start & end points:", x.span())
The output of the above code will be:
First comma start point: 10 First comma end point: 11 First comma start & end points: (10, 11)
The string(), group() & endpos() Methods
In the example below, the usage of string, group() & endpos methods and attributes are described while working with search() function.
import re MyString = "31 January, 28 February, 31 March" #Search a word starting with uppercase "J" x = re.search(r"\bJ\w+", MyString) #print the search string which #is passed to search function print("The search string is:", x.string) #print a part of string in case of match print("Part of matched string:", x.group()) #print the endpos index (which is end of the string) print("The endpos index:", x.endpos)
The output of the above code will be:
The search string is: 31 January, 28 February, 31 March Part of matched string: January The endpos index: 33