I have str1 and str2 below, and I want to use just one regexp which will match both. In case of str1, I also want to be able to capture the number of QSFP ports
>>> str1='''4 48 48-port and 6 QSFP 10GigE Linecard 7548S-LC'''
>>> str2='''4 48 48-port 10GigE Linecard 7548S-LC'''
>>>
I want to be able to capture the numbers "4", "48", "6" (if present), and "7548". But I am unable to capture "6" using the "?" metacharacter.
When I do not use a metacharacter, the capture works for str1, but then I can use this regex because it wont work for str2:
>>> re.search(r'^(\d+)\s+(\d+)\s+.*(?:(\d+)\s+QSFP).*\s+(\d+)S-LC', str1, re.I|re.M).group(3)
'6'
>>>
It works even when I use the "+" to indicate one occurrence, but again, this wont work for str2:
>>> re.search(r'^(\d+)\s+(\d+)\s+.*(?:(\d+)\s+QSFP)+.*\s+(\d+)S-LC', str1, re.I|re.M).group(3)
'6'
>>>
When I use "?" to match for 0 or 1 occurrence, the capture fails even for str1:
>>> re.search(r'^(\d+)\s+(\d+)\s+.*(?:(\d+)\s+QSFP)?.*\s+(\d+)S-LC', str1, re.I|re.M).group(3)
>>>