This question below is different from Regular Expression for duplicate words. The linked question solves finding and returning duplicate matched patterns from a single string, such as "the the ..." or "my question, my answer" which return ['the', 'the'] or ['my', 'my']. My problem is when I use "re.search()" in a for loop and hoping it can return multiple duplicates from multiple strings. My regex search pattern is a number followed by 4 different characters from a-z in small cap. Ex. 2inch. See the code below for what I have tried so far:
import re
names = ['1_abc-2inch-scatter', '1_abc-2inch-uniform', '2_abc-3inch-scatter', '2_abc-3inch-uniform']
sizes = []
for name in names:
sizes = re.search('\d[A-Za-z]+', name).group()
print(sizes)
Output:
['3inch']
Expected output:
['2inch', '2inch', '3inch', '3inch']
I'm not sure why the code seems to overwrite duplicated search results and only returned the last one matched. I want the expected output to be returned. How do I do this?