How to find all cells matching a regex with gspread?

1k Views Asked by At

So I am very new to programming and I am using python gspread module to use a google sheet as a database. There's a function for said module called sheet.findall(query, row, column), and this is great, but there's one issue, the query parameter will only look for an exact match, meaning that if i write "DDG", it will not get me the info from a cell with the value of "DDG-87".

After reading the documentation, I found out that you can use python regular expressions to structure the query parameter, so I did that, but there's a problem; The second parameter in re.findall is WHERE to look for, but the issue is that the whole variable is the action of searching, example shown below:

search = sheet.findall(re.findall("[DDG]", The where to search goes here))

As you can see, the whole variable (SEARCH) is the search function, and therefore, I can not specify where to search.

I have tried to set the second parameter of the regex as (SEARCH), but obviously, it won't work.

Any idea or a clue on how I can set the second parameter of re.findall() to be self, or what I can do so that the function doesn't search for an exact match, but if it contains the text?

Thank you.

1

There are 1 best solutions below

1
On BEST ANSWER

From the gspread docs:

Find all cells matching a regexp:

criteria_re = re.compile(r'(Small|Room-tiering) rug')
cell_list = worksheet.findall(criteria_re)

So the following should work in your case:

criteria_re = re.compile(r'DDG.*')
search = sheet.findall(criteria_re)