I’ve been playing around with some different syntaxes but nothing seems to work, I’ve gotten syntax errors “TypeError: ‘str’ object does not support item assignment” and a few othes
for url in urls:
for i in range(len(urls)):
filename[i] = [re.search(r'/([.]+)(.pdf)') for url[i] in urls]
I’ve also tried:
for url in urls:
filename[i] = [re.search(r'/([.]+)(.pdf)') for url[i] in urls for i in range(len(urls))]
And a few other permutations. I’ve been thinking a list comprehension is the best way to go about doing this, but I’m not sure the syntax I’d need o use to do so.
Any help, please?
You were on the right track with a list comprehension, but you were making it more complex than it is:
Note that this will be a list of matches, if you want, for example, the groups it finds, ignoring any urls it doesn’t match, you could do:
Which uses a nested generator expression to do a check if the match has been made, and extract the groups from it if it has:
Here I made a slight edit to your regex to make it work for my examples, this is done just for the example.
Of course, you could do whatever you want with the match objects here, instead of just getting
match.groups().