-3
LIST = ['ichenbsdr1.chen.com', 'ichenbsds1(SSI15170CCD)',
        'ichenbsds1', 'ichenbsdm2.chen.com',
        'ichenbsdm2.chen.com(ABQB344DEGH)', 'ichenbsdm2']

Need to filter using regex on above list. whichever the index got brackets need to be removed with the information. LIST[1] is 'ichenbsds1(SSI15170CCD)', have to remove "(SSI15170CCD)" and show 'ichenbsds1' alone same as in LIST[4] as well.

I have this regex r'(.*?)\(.*\)' to remove brackets and whatever present inside those brackets. But when i run in the below script its not giving exact output.

sws=[]
for line in LIST:
    Type = re.search(r'(.*?)\(.*\)', line)
    sws.append(Type)
    print (sws)

Expected Output:

['ichenbsdr1.chen.com', 'ichenbsds1', 'ichenbsds1', 'ichenbsdm2.chen.com', 'ichenbsdm2.chen.com', 'ichenbsdm2']
cpander
  • 374
  • 2
  • 9

1 Answers1

1

Use re.sub to remove everything between parenthesis

>>> [re.sub(r'\(.*?\)', '', s) for s in LIST]
['ichenbsdr1.chen.com', 'ichenbsds1', 'ichenbsds1', 'ichenbsdm2.chen.com', 'ichenbsdm2.chen.com', 'ichenbsdm2']
Sunitha
  • 11,777
  • 2
  • 20
  • 23