Python – Regex – from Xpath – TypeError: '_sre.SRE_Match' object is not subscriptable


The following code downloads a webpage, finds and element then runs a regular expression to parse a number out of the string. It seems to work on my python 3.7 test system but not my python 3.5. I am downloading a webpage, finding a text block using Xpath. The xpath returns something like ‘International (21)’ or ‘Books (99)’ I want to extract out the number, the 21 or 99.

In python 3.5 I get back ‘TypeError: ‘_sre.SRE_Match’ object is not subscriptable.’

I am not convinced the error is the differences in versions but that that is the only known diff. The xpath seems to be working as it returns ‘<_sre.SRE_Match object; span=(14, 18), match='(21)’>’ when I print CountObj.

Is there a tweak I should make for python 3.5, is there a better way to code this?

driver = webdriver.Chrome()
driver.get(url); #Download the URL passed from mysql

CatAndCount =  driver.find_element_by_xpath('//h2 [@class="searchResultsTitle"]').text 
 # the above line returns with a name and value like 'International (21)'

CountObj ="((.*?))",CatAndCount)  # look for the number, 21 in example
print (CountObj) # for testing
Asked By: personalt



You need to call the group() method on the re.MatchObject with the number of captured group as the parameter to get that (blank or 0 for the whole match). So, to get the first captured group:


If you have multiple captured groups, and want them all, then use groups() method to get them as a tuple e.g.:


Or if you want specific ones e.g. the 1st and 4th captured group, use group() like below to get a tuple of the asked ones:, 4)
Answered By: heemayl

This TypeError occur because of the lower version, such as below Python Version 3.6

What worked for me on version 3.5 was using .group(index) to access a specific group captured rather than accessing directly.


# Capture Group
result =, target_text) 

# Insert index inside group to access a specific group captured
Answered By: Shah Fahad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.