Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex python remove the left side of text beginning with hyphen or curved brackets

I am stuck on this rather simple regex string issue for hours.

I have a string and I need to remove the left side of text in all occurrence of hyphen "-" or curved brackets "()"
I am able to remove the brackets but I can’t figure out how to remove the hyphen as well.
Below is my attempt

import re
sentence = re.sub(r"\([^()-]*\)", "", 'P. Test data-I am using-(regex)(python)')
print(sentence)

Output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

P. Test data-I am using-

The output I need is

 P. Test data

Thank you for your help.

>Solution :

You can use str.partition:

>>> 'P. Test data-I am using-(regex)(python)'.partition('-')[0]
'P. Test data'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading