2

I want to remove semi-colons, parentheses and dashes at the same time. How to do it with split()? For example, converting (shipment_id[s]-ef234;sender[s]-Taiwan Electronics Co) into ["shipment_id[s]", "ef234", "sender[s]", "Taiwan Electronics Co"] What if the item contain dashes already like"JB HI-FI". How to do with it?

Jody Hou
  • 43
  • 6

1 Answers1

1

It would be super simple to achieve this with regex.

import re
pattern = r'[-|;]'

string = 'shipment_id[s]-ef234;sender[s]-Taiwan Electronics Co'

re.split(pattern, string)
#['shipment_id[s]', 'ef234', 'sender[s]', 'Taiwan Electronics Co']

If the () are included, we can simply add them to our pattern and pass it via a list comprehension to only include values that are True.

string = '(shipment_id[s]-ef234;sender[s]-Taiwan Electronics Co)'
pattern = r'[-|;|\(|\)]'
print([match for match in re.split(pattern, string) if match])

#['shipment_id[s]', 'ef234', 'sender[s]', 'Taiwan Electronics Co']
PacketLoss
  • 5,561
  • 1
  • 9
  • 27