Use the function re.searchalong with the list.
>>> teststr = ['1 FirstString', '2x Sec String', '3rd String', 'x forString', '5X fifth']
>>> [i for i in teststr if re.search(r'\d+[xX]', i) ]
['2x Sec String', '5X fifth']
\d+matches one or more digits. [xX]matches upper and lower case x.
Defining it as a separate function.
>>> def SomeFunc(s):
return [i for i in s if re.search(r'\d+[xX]', i)]
>>> print(SomeFunc(['1 FirstString', '2x Sec String', '3rd String', 'x forString', '5X fifth']))
['2x Sec String', '5X fifth']
source
share